Skip to content

Commit

Permalink
Merge pull request #75 from kondratyev-nv/feature/fix_multiple_teardowns
Browse files Browse the repository at this point in the history
Fix tear down calls
  • Loading branch information
Nikolay Kondratyev authored Apr 6, 2019
2 parents 4ce35ef + 91237ae commit 7b542d1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 27 deletions.
61 changes: 35 additions & 26 deletions src/unittest/unittestScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,46 @@ TEST_RESULT_PREFIX = '${TEST_RESULT_PREFIX}'
STDOUT_LINE = '\\nStdout:\\n%s'
STDERR_LINE = '\\nStderr:\\n%s'
def write_test_state(stream, state, result):
message = base64.b64encode(result[1].encode('utf8')).decode('ascii')
stream.writeln()
stream.writeln("{}:{}:{}:{}".format(TEST_RESULT_PREFIX,
state, result[0].id(), message))
class TextTestResultWithSuccesses(TextTestResult):
def __init__(self, *args, **kwargs):
super(TextTestResultWithSuccesses, self).__init__(*args, **kwargs)
self.successes = []
self.writtenResults = set()
# similar to how unittest.TestResult done capturing
def addSuccess(self, test):
super(TextTestResultWithSuccesses, self).addSuccess(test)
self.successes.append((test, self._execution_info_to_string(test)))
self._mirrorOutput = True
write_test_state(self.stream, "passed", self.successes[-1])
def addError(self, test, err):
super(TextTestResultWithSuccesses, self).addError(test, err)
write_test_state(self.stream, "failed", self.errors[-1])
def addFailure(self, test, err):
super(TextTestResultWithSuccesses, self).addFailure(test, err)
write_test_state(self.stream, "failed", self.failures[-1])
def addSkip(self, test, reason):
super(TextTestResultWithSuccesses, self).addSkip(test, reason)
write_test_state(self.stream, "skipped", self.skipped[-1])
def addExpectedFailure(self, test, err):
super(TextTestResultWithSuccesses, self).addExpectedFailure(test, err)
write_test_state(self.stream, "passed", self.expectedFailures[-1])
def addUnexpectedSuccess(self, test):
super(TextTestResultWithSuccesses, self).addUnexpectedSuccess(test)
write_test_state(self.stream, "failed", self.unexpectedSuccesses[-1])
def _execution_info_to_string(self, test):
msgLines = []
Expand All @@ -40,15 +70,6 @@ class TextTestResultWithSuccesses(TextTestResult):
return ''.join(msgLines)
class TextTestRunnerWithSingleResult(TextTestRunner):
def __init__(self, *args, **kwargs):
super(TextTestRunnerWithSingleResult, self).__init__(*args, buffer = True, **kwargs)
self.single_result = TextTestResultWithSuccesses(self.stream, self.descriptions, self.verbosity)
def _makeResult(self):
return self.single_result
def get_tests(suite):
if hasattr(suite, '__iter__'):
tests = []
Expand All @@ -69,24 +90,12 @@ def filter_by_test_ids(tests, test_ids):
return filter(lambda test: any(test.id().startswith(name) for name in test_ids), tests)
def write_test_state(state, result):
message = base64.b64encode(result[1].encode('utf8')).decode('ascii')
print("{}:{}:{}:{}".format(TEST_RESULT_PREFIX, state, result[0].id(), message))
def run_tests(start_directory, pattern, test_names):
runner = TextTestRunnerWithSingleResult()
tests = [TestSuite([test]) for test in filter_by_test_ids(discover_tests(start_directory, pattern), test_names)]
for test in tests:
result = runner.run(test)
for r in result.skipped:
write_test_state("skipped", r)
for r in result.failures:
write_test_state("failed", r)
for r in result.errors:
write_test_state("failed", r)
for r in result.successes:
write_test_state("passed", r)
runner = TextTestRunner(
buffer=True, resultclass=TextTestResultWithSuccesses, stream=sys.stdout)
tests = filter_by_test_ids(discover_tests(
start_directory, pattern), test_names)
result = runner.run(TestSuite(tests))
action = sys.argv[1]
Expand Down
2 changes: 1 addition & 1 deletion test/pytestGeneral.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ suite('Run pytest tests', () => {
],
}
].forEach(({ suite, cases }) => {
test(`should run ${suite} suite`, async () => {
test(`should run ${suite.label} suite`, async () => {
const mainSuite = await runner.load(config);
expect(mainSuite).to.be.not.undefined;
const suiteToRun = findTestSuiteByLabel(mainSuite!, suite.label, suite.description);
Expand Down
15 changes: 15 additions & 0 deletions test/test_samples/unittest/setup_tests/test_with_setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import unittest


Expand All @@ -6,8 +7,10 @@ class TestWithSetUpClassMethod(unittest.TestCase):
@classmethod
def setUpClass(cls):
if hasattr(cls, 'setUpClass_was_called'):
print('setUpClass was called', cls.setUpClass_was_called)
cls.setUpClass_was_called += 1
else:
print('setUpClass is set')
cls.setUpClass_was_called = 1

def setUp(self):
Expand All @@ -23,3 +26,15 @@ def test_set_up_called_before_test_case1_passed(self):
def test_set_up_called_before_test_case2_passed(self):
assert self.setUpClass_was_called == 1
assert self.setUp_was_called == 1

class TestWithTearDownClassMethod(unittest.TestCase):

@classmethod
def tearDownClass(cls):
cls.tearDown_was_called = True

def test_tear_down_class_was_not_called1_passed(self):
self.assertFalse(hasattr(self, 'tearDown_was_called'))

def test_tear_down_class_was_not_called2_passed(self):
self.assertFalse(hasattr(self, 'tearDown_was_called'))
1 change: 1 addition & 0 deletions test/unittestGeneral.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ suite('Unittest test discovery', () => {
'TestWithSetUpClassMethod',
'AddTests',
'AddTests',
'TestWithTearDownClassMethod',
'EnvironmentVariablesTests'
];
const labels = mainSuite!.children.map(x => x.label);
Expand Down

0 comments on commit 7b542d1

Please sign in to comment.