Qore QUnit Module Reference  0.3.3
 All Classes Namespaces Functions Variables Groups Pages
QUnit Module

Introduction to the QUnit Module

The QUnit module provides a framework for automated testing.

It contains base classes for creating test cases and test suites. It also provides a dependency injection helper for mocking pre-existing classes without modifying their code.

It also provides a number of pre-defined testing functions for use in assertions.

Examples:

1 #!/usr/bin/env qore
2 # -*- mode: qore; indent-tabs-mode: nil -*-
3 
4 %new-style
5 %enable-all-warnings
6 %require-types
7 %strict-args
8 
9 %requires ../../qlib/QUnit.qm
10 
11 #%include ./_some_module_to_test
12 
13 %exec-class QUnitTest
14 
15 public class QUnitTest inherits QUnit::Test {
16  constructor() : Test("QUnitTest", "1.0") {
17  addTestCase("What this method is testing", \testMethod(), NOTHING);
18  addTestCase("Skipped test", \testSkipped(), NOTHING);
19 
20  # Return for compatibility with test harness that checks return value.
21  set_return_value(main());
22  }
23 
24  testMethod() {
25  # Test against success
26  testAssertion("success", \equals(), (True, True));
27  # Test against something else
28  testAssertion("failure", \equals(), (True, False), RESULT_FAILURE);
29  }
30 
31  testSkipped() {
32  # Skip this test
33  testSkip("Because of the reason it skipped");
34  }
35 }
1 #!/usr/bin/env qore
2 # -*- mode: qore; indent-tabs-mode: nil -*-
3 
4 %new-style
5 %enable-all-warnings
6 %require-types
7 %strict-args
8 
9 %requires ../../qlib/QUnit.qm
10 
11 #%include ./_MODULE_TO_TEST_
12 
13 %exec-class MyTestClass
14 
15 public class MyTestClass inherits QUnit::DependencyInjectedTest {
16  constructor() : DependencyInjectedTest("MyTestClass", "1.0") {
17  addTestCase("Test something", \testMethod(), NOTHING);
18 
19  # Mandatory for injected tests
20  setScriptPath(get_script_path());
21  # Return for compatibility with test harness that checks return value.
22  set_return_value(main());
23  }
24 
25  nothing performModuleInjections() {
26  #injectIntoModule("_MODULE_TO_INJECT_INTO_(same as _MODULE_TO_TEST_?)");
27  }
28 
29  nothing performInjections(Program p) {
30  #p.importClass("_class_to_inject_", "_class_to_inject_into_", True);
31  }
32 
33  testMethod() {
34  # Same test style as in TestExample.qtest
35  testAssertion("success", \equals(), (True, True));
36  }
37 }

Running tests

Tests are ran by simply executing the test script:

    qore test.qtest [OPTIONS]

A number of options is available, controlling the behaviour/output of the test

Supported output formats of test reports

Currently the module provides the following output formats:

  • plainquiet - human readable quiet format, prints only failures and a short summary at the end, which is also the default
  • plaintext - human readable format, prints one statement per test
  • junit - machine readable format for further processing

Release Notes

Version 0.3.3

  • improved output in assertion failures for strings with special whitespace and for multi-line data structures (issue 2680)

Version 0.3.2

  • improved error location reporting by providing all stack location information up until the QUnit call to cover the case when multiple code layers are used such as one or more test modules (issue 1720)
  • overloaded testAssertionValue supports auto/number/float
  • more verbose output when number/float difference is found

Version 0.3.1

Version 0.3

  • updated for complex types

Version 0.2

  • fixed showing the assertion location when there are test modules on top of QUnit.qm (issue 1046)

Version 0.1

  • initial version of module