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

Added commands for running tests from a test file #502

Merged
merged 4 commits into from
Apr 12, 2017
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
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