Skip to content

Commit

Permalink
Merge pull request #146 from kondratyev-nv/fix_multiple_unittest_exec…
Browse files Browse the repository at this point in the history
…ution

Fix multiple test execution with similar name
  • Loading branch information
kondratyev-nv authored Apr 23, 2020
2 parents 69c8dc8 + d1c5f9e commit ff99b3f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/unittest/unittestScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,21 @@ def discover_tests(start_directory, pattern):
def filter_by_test_ids(tests, test_ids):
if not test_ids:
return tests
return filter(lambda test: any(test.id().startswith(name) for name in test_ids), tests)
tests_by_id = {}
for test_id in test_ids:
tests_by_id[test_id] = []
for test in tests:
if test.id() in tests_by_id:
tests_by_id[test.id()].append(test)
else:
matching_ids = [test_id for test_id in test_ids if test.id().startswith(test_id + '.')]
for test_id in matching_ids:
tests_by_id[test_id].append(test)
unique_tests = {}
for test_id, matching_tests in tests_by_id.items():
for matching_test in matching_tests:
unique_tests[matching_test.id()] = matching_test
return unique_tests.values()
def run_tests(start_directory, pattern, test_ids):
Expand Down
9 changes: 9 additions & 0 deletions test/test_samples/unittest/other_tests/test_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import unittest

class StringTestWithSimilarNames(unittest.TestCase):
def test_string_passed(self):
self.assertEqual('sample', 'Sample'.lower())

# Test name starts with the same name as above.
def test_string_passed_capitalize_passed(self):
self.assertEqual('Sample', 'sample'.capitalize())
18 changes: 17 additions & 1 deletion test/tests/unittestGeneral.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ suite('Unittest test discovery', () => {
'EnvironmentVariablesTests',
'InvalidTestIdTests_failed',
'test_invalid_import_failed',
'test_invalid_syntax_failed'
'test_invalid_syntax_failed',
'StringTestWithSimilarNames'
];
const labels = mainSuite!.children.map(x => x.label);
expect(labels).to.have.members(expectedSuites);
Expand Down Expand Up @@ -159,6 +160,21 @@ suite('Run unittest tests', () => {
});
});
});

test('should run single test even when test name if the prefix for the other', async () => {
const { suite: mainSuite } = await runner.load(config);
const testToRun = findTestSuiteByLabel(mainSuite!, 'test_string_passed');
expect(testToRun).to.be.not.undefined;
const testToSkip = findTestSuiteByLabel(mainSuite!, 'test_string_passed_capitalize_passed');
expect(testToSkip).to.be.not.undefined;

const states = await runner.run(config, testToRun!.id);
expect(states).to.have.length(1);
const executionResult = states[0];

const expectedState = extractExpectedState(executionResult.test as string);
expect(executionResult.state).to.be.eq(expectedState);
});
});

suite('Unittest run and discovery with start folder in config', () => {
Expand Down

0 comments on commit ff99b3f

Please sign in to comment.