trial.executor.single 34/49(69%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
331
341
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
671
680
690
700
710
720
731
740
750
760
770
781
791
801
810
820
830
840
850
860
870
880
89164
90164
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
106164
107164
1080
109164
110164
1110
112164
1130
114164
115164
1160
117164
1180
119164
1200
121164
1220
123164
1240
1250
1260
1270
1280
1290
130164
131164
1320
133164
1340
1359
1360
1378
1388
1398
1400
1410
1429
1439
1440
1450
146164
147164
148164
149164
1500
151164
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
/++ A module containing the single threaded runner Copyright: © 2017 Szabo Bogdan License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Szabo Bogdan +/ module trial.executor.single; public import trial.interfaces; import trial.runner; import std.datetime; import trial.step; import trial.stackresult; version (Have_fluent_asserts) { version = Have_fluent_asserts_core; } /** The default test executor runs test in sequential order in a single thread */ class DefaultExecutor : ITestExecutor, IStepLifecycleListener, IAttachmentListener { private { SuiteResult suiteResult; TestResult testResult; StepResult currentStep; StepResult[] stepStack; } this() { suiteResult = SuiteResult("unknown"); } /// Called when an attachment is ready void attach(ref const Attachment attachment) { if(currentStep is null) { suiteResult.attachments ~= Attachment(attachment.name, attachment.file, attachment.mime); return; } currentStep.attachments ~= Attachment(attachment.name, attachment.file, attachment.mime); } /// Add the step result and update the other listeners on every step void begin(string suite, string test, ref StepResult step) { currentStep.steps ~= step; stepStack ~= currentStep; currentStep = step; LifeCycleListeners.instance.update(); } /// Update the other listeners on every step void end(string suite, string test, ref StepResult step) { currentStep = stepStack[stepStack.length - 1]; stepStack = stepStack[0 .. $ - 1]; LifeCycleListeners.instance.update(); } /// It does nothing SuiteResult[] beginExecution(ref const(TestCase)[]) { return []; } /// Return the result for the last executed suite SuiteResult[] endExecution() { if (suiteResult.begin == SysTime.fromUnixTime(0)) { return []; } LifeCycleListeners.instance.update(); LifeCycleListeners.instance.end(suiteResult); return [ suiteResult ]; } protected { /// Run a test case void runTest(ref const(TestCase) testCase, TestResult testResult) { try { testCase.func(); testResult.status = TestResult.Status.success; } catch (PendingTestException) { testResult.status = TestResult.Status.pending; } catch (Throwable t) { testResult.status = TestResult.Status.failure; testResult.throwable = t.toTestException; } } /// Convert a test case to a test result void createTestResult(const(TestCase) testCase) { testResult = testCase.toTestResult; testResult.begin = Clock.currTime; testResult.status = TestResult.Status.started; currentStep = testResult; stepStack = []; Step.suite = testCase.suiteName; Step.test = testCase.name; LifeCycleListeners.instance.begin(testCase.suiteName, testResult); runTest(testCase, testResult); testResult.end = Clock.currTime; LifeCycleListeners.instance.end(testCase.suiteName, testResult); } } /// Execute a test case SuiteResult[] execute(ref const(TestCase) testCase) { SuiteResult[] result; LifeCycleListeners.instance.update(); if (suiteResult.name != testCase.suiteName) { if (suiteResult.begin != SysTime.fromUnixTime(0)) { suiteResult.end = Clock.currTime; LifeCycleListeners.instance.end(suiteResult); result = [suiteResult]; } suiteResult = SuiteResult(testCase.suiteName, Clock.currTime, Clock.currTime); LifeCycleListeners.instance.begin(suiteResult); } createTestResult(testCase); suiteResult.tests ~= testResult; currentStep = null; LifeCycleListeners.instance.update(); return result; } } version(unittest) { version(Have_fluent_asserts_core) { import fluent.asserts; } } /// Executing a test case that throws a PendingTestException should mark the test result /// as pending instead of a failure unittest { auto old = LifeCycleListeners.instance; LifeCycleListeners.instance = new LifeCycleListeners; LifeCycleListeners.instance.add(new DefaultExecutor); scope (exit) { LifeCycleListeners.instance = old; } void test() { throw new PendingTestException(); } auto testCase = const TestCase("Some.Suite", "test name", &test, []); auto result = [testCase].runTests; result.length.should.equal(1); result[0].tests[0].status.should.equal(TestResult.Status.pending); } /// Executing a test case should set the right begin and end times unittest { import core.thread; auto old = LifeCycleListeners.instance; LifeCycleListeners.instance = new LifeCycleListeners; LifeCycleListeners.instance.add(new DefaultExecutor); scope (exit) { LifeCycleListeners.instance = old; } void test() { Thread.sleep(1.msecs); } auto testCase = const TestCase("Some.Suite", "test name", &test, []); auto begin = Clock.currTime; auto result = [ testCase ].runTests; auto testResult = result[0].tests[0]; testResult.begin.should.be.greaterThan(begin); testResult.end.should.be.greaterThan(begin + 1.msecs); }