Coverage for src\zapy\test\test_runner.py: 100%

1 import unittest

2 import unittest.case

3 from unittest.runner import TextTestResult

4 from typing import List

5

6 from .models import TestResult

7

8

9 class TestResult(TextTestResult):

10 def __init__(self, *args, **kwargs):

11 super().__init__(*args, **kwargs)

12 self.successes = []

13

14 def addSuccess(self, test):

15 super().addSuccess(test)

16 self.successes.append(test)

17

18 def as_list(self) -> List[TestResult]:

19 successes = [{

20 "method": test._testMethodName,

21 "status": "success",

22 } for test in self.successes]

23 errors = [{

24 "method": test._testMethodName,

25 "traceback": traceback_str,

26 "status": "error",

27 } for test, traceback_str in self.errors]

28 failures = [{

29 "method": test._testMethodName,

30 "traceback": traceback_str,

31 "status": "failure",

32 } for test, traceback_str in self.failures]

33 return successes + errors + failures

34

35

36 def run_tests(*test_classes) -> TestResult:

37 loader = unittest.TestLoader()

38 suites_list = (loader.loadTestsFromTestCase(test_class) for test_class in test_classes)

39 test_suit = unittest.TestSuite(suites_list)

40

41 test_runner = unittest.runner.TextTestRunner(resultclass=TestResult)

42 test_result = test_runner.run(test_suit)

43

44 return test_result