Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for executing Python Tests #20716

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions Tasks/AzureTestPlanV0/Invokers/pythoninvoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import constants = require('../constants');

export async function executePythonTests(testsToBeExecuted: string[]):Promise<number> {
// Perform test discovery
const discoveryArgs: string[] = ['--collect-only'];
const discoveryArgs: string[] = ['--collect-only', '-q'];
const discoveryResult = await runPytestCommand(discoveryArgs);

if (discoveryResult.status !== 0) {
Expand All @@ -14,10 +14,18 @@ export async function executePythonTests(testsToBeExecuted: string[]):Promise<nu

// Extract discovered tests from stdout
const discoveredTests: string[] = extractDiscoveredTests(discoveryResult.stdout ?? '');
testsToBeExecuted = testsToBeExecuted.map(transformTestStrings);

// Find common tests between testsToBeExecuted and discovered tests
const testsToRun: string[] = testsToBeExecuted.filter(test => discoveredTests.indexOf(test) !== -1);

// Variables for debug console logs
const testsToBeExecutedString: string = testsToBeExecuted.join(", ");
const testsToRunString: string = testsToRun.join(", ");

tl.debug(`Tests to executed are: ${testsToBeExecutedString}`);
tl.debug(`Tests to run are: ${testsToRunString}`);

if (testsToRun.length === 0) {
tl.warning("No common tests found between specified tests and discovered tests.");
return 0;
Expand Down Expand Up @@ -57,7 +65,7 @@ async function runPytestCommand(args: string[]): Promise<SpawnResult> {
}
}

function extractDiscoveredTests(output) {
function extractOldDiscoveredTests(output) {
const testNames = [];
let currentPackage = '';
let currentModule = '';
Expand Down Expand Up @@ -87,3 +95,33 @@ function extractDiscoveredTests(output) {
tl.debug("Discovered tests : " + testNames);
return testNames;
}

function extractDiscoveredTests(output: string) {
const testNames = [];

const lines = output.split('\n');

for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if(line && line.includes(".py")){
testNames.push(line);
}
}
tl.debug("Discovered tests : " + testNames);
return testNames;
}

function transformTestStrings(test: string): string {
// Remove any leading or trailing whitespace
test = test.trim();

// Replace '.' with '/' for the directory structure
// Replace the last part of the string with '.py'
test = test.replace(/\./g, '/').replace(/\.([^/]+)$/, '.py');

// Add the `::` before the test function name
const parts = test.split('/');
const functionName = parts.pop(); // Remove the function name
const testFile = parts.join('/'); // Join back the file path
return `${testFile}.py::${functionName}`; // Format as required
}
2 changes: 1 addition & 1 deletion Tasks/AzureTestPlanV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 0,
"Minor": 250,
"Patch": 2
"Patch": 4
},
"preview": true,
"demands": [],
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzureTestPlanV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 0,
"Minor": 250,
"Patch": 2
"Patch": 4
},
"preview": true,
"demands": [],
Expand Down
4 changes: 2 additions & 2 deletions _generated/AzureTestPlanV0.versionmap.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Default|0.250.2
Node20-225|0.250.3
Default|0.250.4
Node20-225|0.250.5
42 changes: 40 additions & 2 deletions _generated/AzureTestPlanV0/Invokers/pythoninvoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import constants = require('../constants');

export async function executePythonTests(testsToBeExecuted: string[]):Promise<number> {
// Perform test discovery
const discoveryArgs: string[] = ['--collect-only'];
const discoveryArgs: string[] = ['--collect-only', '-q'];
const discoveryResult = await runPytestCommand(discoveryArgs);

if (discoveryResult.status !== 0) {
Expand All @@ -14,10 +14,18 @@ export async function executePythonTests(testsToBeExecuted: string[]):Promise<nu

// Extract discovered tests from stdout
const discoveredTests: string[] = extractDiscoveredTests(discoveryResult.stdout ?? '');
testsToBeExecuted = testsToBeExecuted.map(transformTestStrings);

// Find common tests between testsToBeExecuted and discovered tests
const testsToRun: string[] = testsToBeExecuted.filter(test => discoveredTests.indexOf(test) !== -1);

// Variables for debug console logs
const testsToBeExecutedString: string = testsToBeExecuted.join(", ");
const testsToRunString: string = testsToRun.join(", ");

tl.debug(`Tests to executed are: ${testsToBeExecutedString}`);
tl.debug(`Tests to run are: ${testsToRunString}`);

if (testsToRun.length === 0) {
tl.warning("No common tests found between specified tests and discovered tests.");
return 0;
Expand Down Expand Up @@ -57,7 +65,7 @@ async function runPytestCommand(args: string[]): Promise<SpawnResult> {
}
}

function extractDiscoveredTests(output) {
function extractOldDiscoveredTests(output) {
const testNames = [];
let currentPackage = '';
let currentModule = '';
Expand Down Expand Up @@ -87,3 +95,33 @@ function extractDiscoveredTests(output) {
tl.debug("Discovered tests : " + testNames);
return testNames;
}

function extractDiscoveredTests(output: string) {
const testNames = [];

const lines = output.split('\n');

for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if(line && line.includes(".py")){
testNames.push(line);
}
}
tl.debug("Discovered tests : " + testNames);
return testNames;
}

function transformTestStrings(test: string): string {
// Remove any leading or trailing whitespace
test = test.trim();

// Replace '.' with '/' for the directory structure
// Replace the last part of the string with '.py'
test = test.replace(/\./g, '/').replace(/\.([^/]+)$/, '.py');

// Add the `::` before the test function name
const parts = test.split('/');
const functionName = parts.pop(); // Remove the function name
const testFile = parts.join('/'); // Join back the file path
return `${testFile}.py::${functionName}`; // Format as required
}
6 changes: 3 additions & 3 deletions _generated/AzureTestPlanV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 0,
"Minor": 250,
"Patch": 2
"Patch": 4
},
"preview": true,
"demands": [],
Expand Down Expand Up @@ -186,8 +186,8 @@
"MultipleMatchingGradlewFound": "Multiple gradlew files found. Selecting the first matched instance"
},
"_buildConfigMapping": {
"Default": "0.250.2",
"Default": "0.250.4",
"LocalPackages": "0.249.4",
"Node20-225": "0.250.3"
"Node20-225": "0.250.5"
}
}
6 changes: 3 additions & 3 deletions _generated/AzureTestPlanV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 0,
"Minor": 250,
"Patch": 2
"Patch": 4
},
"preview": true,
"demands": [],
Expand Down Expand Up @@ -186,8 +186,8 @@
"MultipleMatchingGradlewFound": "ms-resource:loc.messages.MultipleMatchingGradlewFound"
},
"_buildConfigMapping": {
"Default": "0.250.2",
"Default": "0.250.4",
"LocalPackages": "0.249.4",
"Node20-225": "0.250.3"
"Node20-225": "0.250.5"
}
}
42 changes: 40 additions & 2 deletions _generated/AzureTestPlanV0_Node20/Invokers/pythoninvoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import constants = require('../constants');

export async function executePythonTests(testsToBeExecuted: string[]):Promise<number> {
// Perform test discovery
const discoveryArgs: string[] = ['--collect-only'];
const discoveryArgs: string[] = ['--collect-only', '-q'];
const discoveryResult = await runPytestCommand(discoveryArgs);

if (discoveryResult.status !== 0) {
Expand All @@ -14,10 +14,18 @@ export async function executePythonTests(testsToBeExecuted: string[]):Promise<nu

// Extract discovered tests from stdout
const discoveredTests: string[] = extractDiscoveredTests(discoveryResult.stdout ?? '');
testsToBeExecuted = testsToBeExecuted.map(transformTestStrings);

// Find common tests between testsToBeExecuted and discovered tests
const testsToRun: string[] = testsToBeExecuted.filter(test => discoveredTests.indexOf(test) !== -1);

// Variables for debug console logs
const testsToBeExecutedString: string = testsToBeExecuted.join(", ");
const testsToRunString: string = testsToRun.join(", ");

tl.debug(`Tests to executed are: ${testsToBeExecutedString}`);
tl.debug(`Tests to run are: ${testsToRunString}`);

if (testsToRun.length === 0) {
tl.warning("No common tests found between specified tests and discovered tests.");
return 0;
Expand Down Expand Up @@ -57,7 +65,7 @@ async function runPytestCommand(args: string[]): Promise<SpawnResult> {
}
}

function extractDiscoveredTests(output) {
function extractOldDiscoveredTests(output) {
const testNames = [];
let currentPackage = '';
let currentModule = '';
Expand Down Expand Up @@ -87,3 +95,33 @@ function extractDiscoveredTests(output) {
tl.debug("Discovered tests : " + testNames);
return testNames;
}

function extractDiscoveredTests(output: string) {
const testNames = [];

const lines = output.split('\n');

for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if(line && line.includes(".py")){
testNames.push(line);
}
}
tl.debug("Discovered tests : " + testNames);
return testNames;
}

function transformTestStrings(test: string): string {
// Remove any leading or trailing whitespace
test = test.trim();

// Replace '.' with '/' for the directory structure
// Replace the last part of the string with '.py'
test = test.replace(/\./g, '/').replace(/\.([^/]+)$/, '.py');

// Add the `::` before the test function name
const parts = test.split('/');
const functionName = parts.pop(); // Remove the function name
const testFile = parts.join('/'); // Join back the file path
return `${testFile}.py::${functionName}`; // Format as required
}
6 changes: 3 additions & 3 deletions _generated/AzureTestPlanV0_Node20/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 0,
"Minor": 250,
"Patch": 3
"Patch": 5
},
"preview": true,
"demands": [],
Expand Down Expand Up @@ -190,8 +190,8 @@
"MultipleMatchingGradlewFound": "Multiple gradlew files found. Selecting the first matched instance"
},
"_buildConfigMapping": {
"Default": "0.250.2",
"Default": "0.250.4",
"LocalPackages": "0.249.4",
"Node20-225": "0.250.3"
"Node20-225": "0.250.5"
}
}
6 changes: 3 additions & 3 deletions _generated/AzureTestPlanV0_Node20/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 0,
"Minor": 250,
"Patch": 3
"Patch": 5
},
"preview": true,
"demands": [],
Expand Down Expand Up @@ -190,8 +190,8 @@
"MultipleMatchingGradlewFound": "ms-resource:loc.messages.MultipleMatchingGradlewFound"
},
"_buildConfigMapping": {
"Default": "0.250.2",
"Default": "0.250.4",
"LocalPackages": "0.249.4",
"Node20-225": "0.250.3"
"Node20-225": "0.250.5"
}
}