Skip to content

Commit

Permalink
Added commands for running tests from a test file (DonJayamanne#502)
Browse files Browse the repository at this point in the history
* Added commands for running tests from a test file

* Corrected unintentional committed change

* Added missing newline
  • Loading branch information
aschlapsi authored and amos402 committed Apr 25, 2017
1 parent 1558b9d commit 874e994
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
"onCommand:python.viewTestOutput",
"onCommand:python.selectAndRunTestMethod",
"onCommand:python.selectAndDebugTestMethod",
"onCommand:python.selectAndRunTestFile",
"onCommand:python.runCurrentTestFile",
"onCommand:python.runFailedTests",
"onCommand:python.execSelectionInTerminal",
"onCommand:python.execSelectionInDjangoShell",
Expand Down Expand Up @@ -161,6 +163,16 @@
"title": "Debug Unit Test Method ...",
"category": "Python"
},
{
"command": "python.selectAndRunTestFile",
"title": "Run Unit Test File ...",
"category": "Python"
},
{
"command": "python.runCurrentTestFile",
"title": "Run Current Unit Test File",
"category": "Python"
},
{
"command": "python.runFailedTests",
"title": "Run Failed Unit Tests",
Expand Down
2 changes: 2 additions & 0 deletions src/client/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export namespace Commands {
export const Tests_ViewOutput = 'python.viewTestOutput';
export const Tests_Select_And_Run_Method = 'python.selectAndRunTestMethod';
export const Tests_Select_And_Debug_Method = 'python.selectAndDebugTestMethod';
export const Tests_Select_And_Run_File = 'python.selectAndRunTestFile';
export const Tests_Run_Current_File = 'python.runCurrentTestFile';
export const Refactor_Extract_Variable = 'python.refactorExtractVariable';
export const Refaactor_Extract_Method = 'python.refactorExtractMethod';
export const Update_SparkLibrary = 'python.updateSparkLibrary';
Expand Down
38 changes: 37 additions & 1 deletion src/client/unittests/display/picker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { QuickPickItem, window } from 'vscode';
import * as vscode from 'vscode';
import { Tests, TestFunction, FlattenedTestFunction, TestStatus } from '../common/contracts';
import { Tests, TestFile, TestFunction, FlattenedTestFunction, TestStatus } from '../common/contracts';
import { getDiscoveredTests } from '../common/testUtils';
import * as constants from '../../common/constants';
import * as path from 'path';
Expand Down Expand Up @@ -30,6 +30,17 @@ export class TestDisplay {
}, reject);
});
}
public selectTestFile(rootDirectory: string, tests: Tests): Promise<TestFile> {
return new Promise<TestFile>((resolve, reject) => {
window.showQuickPick(buildItemsForTestFiles(rootDirectory, tests.testFiles), { matchOnDescription: true, matchOnDetail: true })
.then(item => {
if (item && item.testFile) {
return resolve(item.testFile);
}
return reject();
}, reject);
});
}
public displayFunctionTestPickerUI(rootDirectory: string, fileName: string, testFunctions: TestFunction[], debug?: boolean) {
const tests = getDiscoveredTests();
if (!tests) {
Expand Down Expand Up @@ -73,6 +84,10 @@ interface TestItem extends QuickPickItem {
type: Type;
fn?: FlattenedTestFunction;
}
interface TestFileItem extends QuickPickItem {
type: Type;
testFile?: TestFile;
}
function getSummary(tests?: Tests) {
if (!tests || !tests.summary) {
return '';
Expand Down Expand Up @@ -150,6 +165,27 @@ function buildItemsForFunctions(rootDirectory: string, tests: FlattenedTestFunct
});
return functionItems;
}
function buildItemsForTestFiles(rootDirectory: string, testFiles: TestFile[]): TestFileItem[] {
let fileItems: TestFileItem[] = testFiles.map(testFile => {
return {
description: '',
detail: path.relative(rootDirectory, testFile.fullPath),
type: Type.RunFile,
label: path.basename(testFile.fullPath),
testFile: testFile
}
})
fileItems.sort((a, b) => {
if (a.detail < b.detail) {
return -1;
}
if (a.detail > b.detail) {
return 1;
}
return 0;
})
return fileItems;
}
function onItemSelected(selection: TestItem, debug?: boolean) {
if (!selection || typeof selection.type !== 'number') {
return;
Expand Down
35 changes: 35 additions & 0 deletions src/client/unittests/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ function registerCommands(): vscode.Disposable[] {
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_Ask_To_Stop_Test, () => displayStopUI('Stop running tests')));
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_Select_And_Run_Method, () => selectAndRunTestMethod()));
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_Select_And_Debug_Method, () => selectAndRunTestMethod(true)));
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_Select_And_Run_File, () => selectAndRunTestFile()));
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_Run_Current_File, () => runCurrentTestFile()));

return disposables;
}
Expand Down Expand Up @@ -105,6 +107,39 @@ function selectAndRunTestMethod(debug?: boolean) {
}).catch(() => { });
});
}
function selectAndRunTestFile() {
let testManager = getTestRunner();
if (!testManager) {
return displayTestFrameworkError(outChannel);
}
testManager.discoverTests(true, true).then(() => {
const tests = getDiscoveredTests();
testDisplay = testDisplay ? testDisplay : new TestDisplay();
testDisplay.selectTestFile(vscode.workspace.rootPath, tests).then(testFile => {
runTestsImpl({testFile: [testFile]});
}).catch(() => { });
});
}
function runCurrentTestFile() {
if (!vscode.window.activeTextEditor) {
return;
}
const currentFilePath = vscode.window.activeTextEditor.document.fileName;
let testManager = getTestRunner();
if (!testManager) {
return displayTestFrameworkError(outChannel);
}
testManager.discoverTests(true, true).then(() => {
const tests = getDiscoveredTests();
const testFiles = tests.testFiles.filter(testFile => {
return testFile.fullPath == currentFilePath;
});
if (testFiles.length < 1) {
return;
}
runTestsImpl({testFile: [testFiles[0]]});
});
}
function displayStopUI(message: string) {
let testManager = getTestRunner();
if (!testManager) {
Expand Down

0 comments on commit 874e994

Please sign in to comment.