@@ -14,10 +14,25 @@ export async function executePythonTests(testsToBeExecuted: string[]):Promise<nu
14
14
15
15
// Extract discovered tests from stdout
16
16
const discoveredTests : string [ ] = extractDiscoveredTests ( discoveryResult . stdout ?? '' ) ;
17
- testsToBeExecuted = testsToBeExecuted . map ( transformTestStrings ) ;
17
+ var testStringtoFQNMap : Map < string , string > = new Map < string , string > ( ) ;
18
+
19
+ for ( let test of discoveredTests ) {
20
+ testStringtoFQNMap . set ( transformTestStrings ( test ) , test ) ;
21
+ }
22
+
23
+ var testsToRun : string [ ] = [ ] ;
24
+
25
+ for ( let test of testsToBeExecuted ) {
26
+ if ( ! testStringtoFQNMap . has ( test ) ) {
27
+ tl . debug ( `Test ${ test } not found in discovered tests` ) ;
28
+ }
29
+ else {
30
+ testsToRun . push ( testStringtoFQNMap . get ( test ) ) ;
31
+ }
32
+ }
18
33
19
34
// Find common tests between testsToBeExecuted and discovered tests
20
- const testsToRun : string [ ] = testsToBeExecuted . filter ( test => discoveredTests . indexOf ( test ) !== - 1 ) ;
35
+ // const testsToRun: string[] = testsToBeExecuted.filter(test => discoveredTests.indexOf(test) !== -1);
21
36
22
37
// Variables for debug console logs
23
38
const testsToBeExecutedString : string = testsToBeExecuted . join ( ", " ) ;
@@ -65,41 +80,9 @@ async function runPytestCommand(args: string[]): Promise<SpawnResult> {
65
80
}
66
81
}
67
82
68
- function extractOldDiscoveredTests ( output ) {
69
- const testNames = [ ] ;
70
- let currentPackage = '' ;
71
- let currentModule = '' ;
72
- let currentClass = '' ;
73
-
74
- const lines = output . split ( '\n' ) ;
75
-
76
- for ( let i = 0 ; i < lines . length ; i ++ ) {
77
- const line = lines [ i ] . trim ( ) ;
78
-
79
- if ( line . startsWith ( '<Package' ) ) {
80
- currentPackage = line . match ( / < P a c k a g e ( .* ?) > / ) [ 1 ] ;
81
- } else if ( line . startsWith ( '<Module' ) ) {
82
- currentModule = line . match ( / < M o d u l e ( .* ?) > / ) [ 1 ] ;
83
- } else if ( line . startsWith ( '<UnitTestCase' ) ) {
84
- currentClass = line . match ( / < U n i t T e s t C a s e ( .* ?) > / ) [ 1 ] ;
85
- } else if ( line . startsWith ( '<TestCaseFunction' ) ) {
86
- const functionName = line . match ( / < T e s t C a s e F u n c t i o n ( .* ?) > / ) [ 1 ] ;
87
- let fullyQualifiedName = '' ;
88
- if ( currentPackage !== '' ) {
89
- fullyQualifiedName += currentPackage + '/' ;
90
- }
91
- fullyQualifiedName += `${ currentModule } ::${ currentClass } ::${ functionName } ` ;
92
- testNames . push ( fullyQualifiedName ) ;
93
- }
94
- }
95
- tl . debug ( "Discovered tests : " + testNames ) ;
96
- return testNames ;
97
- }
98
-
99
- function extractDiscoveredTests ( output : string ) {
100
- const testNames = [ ] ;
101
-
102
- const lines = output . split ( '\n' ) ;
83
+ function extractDiscoveredTests ( output : string ) : string [ ] {
84
+ var testNames : string [ ] = [ ] ;
85
+ var lines : string [ ] = output . split ( '\n' ) ;
103
86
104
87
for ( let i = 0 ; i < lines . length ; i ++ ) {
105
88
const line = lines [ i ] . trim ( ) ;
@@ -111,17 +94,29 @@ function extractDiscoveredTests(output: string) {
111
94
return testNames ;
112
95
}
113
96
114
- function transformTestStrings ( test : string ) : string {
97
+ // Input is like Folder/SubFolder/TestClass.py::TestSubClass::TestSubSubClass::test_method_name
98
+ // Output is lke Folder.SubFolder.TestClass.TestSubClass.TestSubSubClass.test_method_name
99
+ function transformTestStrings ( automatedTestName : string ) : string {
115
100
// Remove any leading or trailing whitespace
116
- test = test . trim ( ) ;
101
+ automatedTestName = automatedTestName . trim ( ) ;
102
+ let updatedAutomatedTestName : string = automatedTestName ;
103
+
104
+ const index = automatedTestName . indexOf ( "::" ) ;
105
+ if ( index !== - 1 ) {
106
+ let testFilePath = automatedTestName . substring ( 0 , index ) ;
107
+ let testMethod = automatedTestName . substring ( index + 2 ) ;
108
+
109
+ //Check if testfilePath is a python file
110
+ if ( testFilePath . endsWith ( ".py" ) ) {
111
+ testFilePath = testFilePath . slice ( 0 , - 3 ) . replace ( / \/ / g, '.' ) ;
117
112
118
- // Replace '.' with '/' for the directory structure
119
- // Replace the last part of the string with '.py'
120
- test = test . replace ( / \. / g, '/' ) . replace ( / \. ( [ ^ / ] + ) $ / , '.py' ) ;
113
+ //Do the same replace with :: to . in testMethod
114
+ testMethod = testMethod . replace ( / : : / g, '.' ) ;
115
+
116
+ //Finally generate updatedAutomatedTestName
117
+ updatedAutomatedTestName = testFilePath + "." + testMethod ;
118
+ }
119
+ }
121
120
122
- // Add the `::` before the test function name
123
- const parts = test . split ( '/' ) ;
124
- const functionName = parts . pop ( ) ; // Remove the function name
125
- const testFile = parts . join ( '/' ) ; // Join back the file path
126
- return `${ testFile } .py::${ functionName } ` ; // Format as required
121
+ return updatedAutomatedTestName ;
127
122
}
0 commit comments