Class UnitTestDiscovery
The default test discovery looks for unit test sections and groups them by module
Methods
Name | Description |
---|---|
getTestCases
()
|
Get the test cases from the compiled source code |
Example
It should extract the line from the default test name
extractLine("__unittest_runTestsOnDevices_133_0()") .should .equal(133);
Example
It should extract the line from the default test name with d_ in symbol name
extractLine("__unittest_runTestsOnDevices_d_133_0()") .should .equal(133);
Example
It should find this test
auto testDiscovery = new UnitTestDiscovery;
testDiscovery .addModule!(__FILE__, "trial.discovery.unit");
testDiscovery .testCases .keys .should .contain("trial.discovery.unit");
testDiscovery .testCases["trial.discovery.unit"] .values .map!"a.name" .should .contain(
"It should find this test");
Example
It should find this flaky test
auto testDiscovery = new UnitTestDiscovery;
testDiscovery .addModule!(__FILE__, "trial.discovery.unit");
testDiscovery .testCases .keys .should .contain("trial.discovery.unit");
auto r = testDiscovery .testCases["trial.discovery.unit"] .values .filter!(
a => a .name == "It should find this flaky test");
r .empty .should .equal(false) .because("a flaky test is in this module");
r .front .labels .map!(a => a .name) .should .equal(["status_details"]);
r .front .labels[0] .value .should .equal("flaky");
Example
It should find the line of this test
enum line = __LINE__ - 2;
auto testDiscovery = new UnitTestDiscovery;
testDiscovery .addModule!(__FILE__, "trial.discovery.unit");
testDiscovery .testCases .keys .should .contain("trial.discovery.unit");
auto r = testDiscovery .testCases["trial.discovery.unit"] .values .filter!(
a => a .name == "It should find the line of this test");
r .empty .should .equal(false) .because("the location should be present");
r .front .location .fileName .should .endWith("unit.d");
r .front .location .line .should .equal(line);
Example
It should find this test with issues attributes
auto testDiscovery = new UnitTestDiscovery;
testDiscovery .addModule!(__FILE__, "trial.discovery.unit");
testDiscovery .testCases .keys .should .contain("trial.discovery.unit");
auto r = testDiscovery .testCases["trial.discovery.unit"] .values .filter!(
a => a .name == "It should find this test with issues attributes");
r .empty .should .equal(false) .because("an issue test is in this module");
r .front .labels .map!(a => a .name) .should .equal(["issue", "issue"]);
r .front .labels .map!(a => a .value) .should .equal(["1", "2"]);
Example
The discoverTestCases should find the test with issues attributes
immutable line = __LINE__ - 2;
auto testDiscovery = new UnitTestDiscovery;
auto tests = testDiscovery .discoverTestCases(__FILE__);
tests .length .should .be .greaterThan(0);
auto testFilter = tests .filter!(a => a .name == "It should find this test with issues attributes");
testFilter .empty .should .equal(false);
auto theTest = testFilter .front;
theTest .labels .map!(a => a .name) .should .equal(["issue", "issue"]);
theTest .labels .map!(a => a .value) .should .equal(["1", "2"]);
Example
The discoverTestCases should find the test with the flaky attribute
immutable line = __LINE__ - 2;
auto testDiscovery = new UnitTestDiscovery;
auto tests = testDiscovery .discoverTestCases(__FILE__);
tests .length .should .be .greaterThan(0);
auto testFilter = tests .filter!(a => a .name == "It should find this flaky test");
testFilter .empty .should .equal(false);
auto theTest = testFilter .front;
theTest .labels .map!(a => a .name) .should .equal(["status_details"]);
theTest .labels .map!(a => a .value) .should .equal(["flaky"]);
Example
The discoverTestCases should find this test
immutable line = __LINE__ - 2;
auto testDiscovery = new UnitTestDiscovery;
auto tests = testDiscovery .discoverTestCases(__FILE__);
tests .length .should .be .greaterThan(0);
auto testFilter = tests .filter!(a => a .name == "The discoverTestCases should find this test");
testFilter .empty .should .equal(false);
auto thisTest = testFilter .front;
thisTest .suiteName .should .equal("trial.discovery.unit");
thisTest .location .fileName .should .equal(__FILE__);
thisTest .location .line .should .equal(line);
Example
discoverTestCases should ignore version(unittest)
auto testDiscovery = new UnitTestDiscovery;
auto tests = testDiscovery .discoverTestCases(__FILE__);
tests .length .should .be .greaterThan(0);
auto testFilter = tests .filter!(a => a .name == "This adds asserts to the module");
testFilter .empty .should .equal(true);
Example
discoverTestCases should find the same tests like testCases
auto testDiscovery = new UnitTestDiscovery;
testDiscovery .addModule!(__FILE__, "trial.discovery.unit");
auto allTests = testDiscovery
.getTestCases
.sort!((a, b) => a .location .line < b .location .line)
.array;
testDiscovery
.discoverTestCases(__FILE__) .map!(a => a .toString) .join("\n")
.should .equal(allTests .map!(a => a .toString) .join("\n"));