Skip to content

Commit ee596a6

Browse files
committed
Fix discovery of PyTests with packages inbetween folders
Fixes microsoft#3936
1 parent 4c2655f commit ee596a6

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

news/2 Fixes/3936.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix discovery of PyTest tests when package folders are inbetween regular test folders (thanks [Alexander Grund](https://github.com/Flamefire))

src/client/unittests/pytest/services/parserService.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,33 @@ export class TestsParser implements ITestsParser {
3030

3131
let haveErrors = false;
3232

33-
let packagePrefix: string = '';
33+
const packages: { indent: number; packagePrefix: string }[] = [];
3434
content.split(/\r?\n/g).forEach((line, index, lines) => {
3535
if (options.token && options.token.isCancellationRequested) {
3636
return;
3737
}
3838

3939
const trimmedLine: string = line.trim();
4040

41-
if (trimmedLine.startsWith('<Package \'')) {
41+
if (trimmedLine.startsWith('<Package \'') || trimmedLine.startsWith('<Module \'') || index === lines.length - 1) {
42+
let packagePrefix : string = '';
43+
if (packages.length > 0) {
44+
packagePrefix = packages[packages.length - 1].packagePrefix;
45+
}
4246
// Process the previous lines.
4347
this.parsePyTestModuleCollectionResult(options.cwd, logOutputLines, testFiles, parentNodes, packagePrefix);
48+
let indent = line.indexOf('<');
49+
while (packages.length > 0 && packages[packages.length - 1].indent >= indent) {
50+
packages.pop();
51+
}
4452
logOutputLines = [''];
45-
46-
packagePrefix = this.extractPackageName(trimmedLine, options.cwd);
53+
if (trimmedLine.startsWith('<Package \'')) {
54+
indent = line.indexOf('<');
55+
packagePrefix = this.extractPackageName(trimmedLine, options.cwd);
56+
packages.push({indent: indent, packagePrefix: packagePrefix});
57+
}
4758
}
4859

49-
if (trimmedLine.startsWith('<Module \'') || index === lines.length - 1) {
50-
// Process the previous lines.
51-
this.parsePyTestModuleCollectionResult(options.cwd, logOutputLines, testFiles, parentNodes, packagePrefix);
52-
logOutputLines = [''];
53-
}
5460
if (errorLine.test(line)) {
5561
haveErrors = true;
5662
logOutputLines = [''];

src/test/unittests/pytest/pytest_unittest_parser_data.ts

+27
Original file line numberDiff line numberDiff line change
@@ -1383,5 +1383,32 @@ export const pytestScenarioData: PytestDiscoveryScenario[] =
13831383
"",
13841384
"======================== no tests ran in 0.36 seconds ========================="
13851385
]
1386+
},
1387+
{
1388+
pytest_version_spec: ">= 3.7",
1389+
platform: PytestDataPlatformType.NonWindows,
1390+
description: "With package inbetween.",
1391+
rootdir: "/home/user/test/pytest_scenario",
1392+
test_functions: [
1393+
"aTest/test_upper.py::test_upper_cmd",
1394+
"bPackage/testPackage.py::test_package",
1395+
"zTest/test_lower.py::test_lower_cmd"
1396+
],
1397+
functionCount: 3,
1398+
stdout: [
1399+
"============================= test session starts =============================",
1400+
"platform win32 -- Python 3.7.0, pytest-3.7.4, py-1.6.0, pluggy-0.7.1",
1401+
"rootdir: /home/user/test/pytest_scenario, inifile:",
1402+
"collected 3 items",
1403+
"<Module 'aTest/test_upper.py'>",
1404+
" <Function 'test_upper_cmd'>",
1405+
"<Package '/home/user/test/pytest_scenario/bPackage'>",
1406+
" <Module 'testPackage.py'>",
1407+
" <Function 'test_package'>",
1408+
"<Module 'zTest/test_lower.py'>",
1409+
" <Function 'test_lower_cmd'>",
1410+
"",
1411+
"======================== no tests ran in 0.36 seconds ========================="
1412+
]
13861413
}
13871414
];

0 commit comments

Comments
 (0)