Skip to content

Instantly share code, notes, and snippets.

@krishnadey30
Last active June 17, 2019 23:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krishnadey30/7de88923f7b13c3748797d8c07c9ffda to your computer and use it in GitHub Desktop.
Save krishnadey30/7de88923f7b13c3748797d8c07c9ffda to your computer and use it in GitHub Desktop.
This gist holds the initial draft of TestResult class for Chapel's UnitTest Framework.
/*
  Holder for test result information.
  Test results are automatically managed by the TestRunner, and do not 
  need to be explicitly manipulated by writers of tests.  
  Each instance holds the total number of tests run, and collections of 
  failures and errors that occurred among those test runs. The collections
  contain tuples of (testcase, exceptioninfo), where exceptioninfo is the
  formatted traceback of the error that occurred.
*/
class TestResult {
  type tupType = 2*string;
  var failures: [1..0] tupType,
      errors: [1..0] tupType,
      skipped: [1..0] tupType;
  var testsRun = 0;
  var shouldStop = false;
  var separator1 = "="* 70,
      separator2 = "-"* 70;
  // called when a test if about to run
  proc startTest() {
    this.testsRun += 1;
  }
  
  /*Called when an error has occurred.*/
  proc addError(test, errMsg) {
    this.errors.push_back((test: string, errMsg: string));
  }

  /*called when error occured */
  proc addFailure(test, errMsg) {
    this.failures.push_back((test: string, errMsg: string));
  }

  /*Called when a test has completed successfully*/
  proc addSuccess(test) { }

  /*Called when a test is skipped.*/
  proc addSkip(test, reason) {
    this.skipped.push_back((test: string, reason: string));
  }

  /*Tells whether or not this result was a success.*/
  proc wasSuccessful() {
    return this.failures.size == 0 && this.errors.size == 0;
  }
  
  /* Indicates that the tests should be aborted. */
  proc stop() {
    this.shouldStop = true;
  }

  /*Count of test skipped*/
  proc skippedTests() {
    return this.skipped.size;
  }

  /*Count of test failed*/
  proc failedTests() {
    return this.failures.size;
  }

  /*Count of tests giving error*/
  proc erroredTests() {
    return this.errors.size;
  }

  proc printErrors() {
    writeln();
    this.printErrorList("ERROR", this.errors);
    this.printErrorList("FAIL", this.failures);
    this.printErrorList("SKIPPED", this.skipped);
  }

  proc printErrorList(flavour, errors) {
    for (test, err) in errors {
      writeln(this.separator1);
      writeln(flavour," ",test);
      writeln(this.separator2);
      writeln(err);
    }
  }

  /* Function to print the result*/
  proc PrintResult() {
    var skipped = this.skippedTests();
    var run = this.testsRun - skipped;
    writeln("Run "+ run +" "+printTest(run));
    writeln();
    var infos: [1..0](string);
    if !this.wasSuccessful() {
      write("FAILED");
      var failed = this.failedTests(),
        errored = this.erroredTests();
      if failed then
        infos.push_back("failures = "+failed);
      if errored then
        infos.push_back("errors = "+errored);
    }
    else
      stdout.write("OK");
    if skipped then
      infos.push_back("skipped = "+skipped);
    if infos.size {
      stdout.write(" ");
      for info in infos do stdout.write(info," ");
    }
    stdout.write("\n");
      // for value in this.failures do stdout.writeln(value);
      // for value in this.skipped do stdout.writeln(value);
  }

  proc printTest(count) {
    if count > 1 {
      return "tests";
    }
    return "test";
  }
}
@ben-albrecht
Copy link

Hey Krishna, I'm liking this design so far. Thanks for putting it together.

Some feedback

Note that comments should go just above the identifier for chpldoc purposes, e.g.

  /*
  Holder for test result information.
  Test results are automatically managed by the TestRunner, and do not 
  need to be explicitly manipulated by writers of tests.  
  Each instance holds the total number of tests run, and collections of 
  failures and errors that occurred among those test runs. The collections
  contain tuples of (testcase, exceptioninfo), where exceptioninfo is the
  formatted traceback of the error that occurred.
  */
  class TestResult { ... }
  var failures: LinkedList(tupType),
      errors: LinkedList(tupType),
      skipped: LinkedList(tupType);

Let's make these arrays, e.g.

  var failures: [1..0] tupType,
      errors: [1..0] tupType,
      skipped: [1..0] tupType;
  proc addError(test, errMsg) {
    /*Called when an error has occurred.*/
    this.errors.push_back((test: string, errMsg: string));
  }

I wonder if this should store the actual error instead of just the error message. The only advantage I can think of is for cases where the error doesn't actually have an error message (it could be thrown from some library method that was called), and we need a way to distinguish it.

I'm sure we'll be adding a lot of new methods, but an obvious next step seems like counting the skipped tests.

@krishnadey30
Copy link
Author

What I meant here with error message is the message we get from errors like AssertionError,TestSkippedError etc.

@krishnadey30
Copy link
Author

I have made modification as suggested and also added two new methods skippedTests and failedTests.

@ben-albrecht
Copy link

Looks good to me. Ping me if you have further changes you'd like review on.

@krishnadey30
Copy link
Author

Sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment