trial.executor.single 71/71(100%) 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
2913
3013
310
320
330
340
354
361
371
380
390
403
410
420
430
440
450
4611
4711
4811
4911
500
510
520
530
540
5511
5611
5711
580
590
600
610
620
639
640
650
660
670
680
6912
700
711
720
730
7411
7511
7611
770
780
790
800
810
820
830
840
85229
86227
870
880
890
901
910
920
930
941
951
960
970
980
990
1000
1010
102234
103234
1040
105234
106234
1070
108234
1090
110234
111234
1120
113234
1140
115234
1160
117234
1180
119234
1200
1210
1220
1230
1240
1250
126234
127234
1280
129234
1300
13153
1320
13341
13441
13541
1360
1370
13853
13953
1400
1410
142234
143234
144234
145234
1460
147234
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1601
1611
1621
1630
1640
1651
1660
1670
1681
1691
1700
1710
1721
1731
1740
1752
1762
1770
1780
1790
1800
1810
1820
1831
1841
1851
1860
1870
1881
1890
1900
1911
1921
1930
1940
1951
1960
1971
1981
1991
2000
2012
2022
2030
/++ 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; /** 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) { 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); }