diff --git a/src/unittest/unittestScripts.ts b/src/unittest/unittestScripts.ts index d6de491..a3f9407 100644 --- a/src/unittest/unittestScripts.ts +++ b/src/unittest/unittestScripts.ts @@ -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): diff --git a/test/test_samples/unittest/other_tests/test_string.py b/test/test_samples/unittest/other_tests/test_string.py new file mode 100644 index 0000000..b77811a --- /dev/null +++ b/test/test_samples/unittest/other_tests/test_string.py @@ -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()) diff --git a/test/tests/unittestGeneral.test.ts b/test/tests/unittestGeneral.test.ts index c138f8e..125b6ac 100644 --- a/test/tests/unittestGeneral.test.ts +++ b/test/tests/unittestGeneral.test.ts @@ -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); @@ -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', () => {