Skip to content

Fix discovery for pytest >= 4.1.1 #48

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

Merged
merged 1 commit into from
Jan 14, 2019
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
],
"keywords": [
"python",
"unittest",
"pytest",
"test",
"testing"
"testing",
"unittest",
"pytest"
],
"scripts": {
"postinstall": "node ./node_modules/vscode/bin/install",
Expand Down
178 changes: 57 additions & 121 deletions src/pytest/pytestTestCollectionParser.ts
Original file line number Diff line number Diff line change
@@ -1,141 +1,77 @@
import * as path from 'path';
import { TestInfo, TestSuiteInfo } from 'vscode-test-adapter-api';

import { empty } from '../utilities';
import { empty, getTestOutputBySplittingString, groupBy } from '../utilities';

export function parseTestSuites(content: string, cwd: string): Array<TestSuiteInfo | TestInfo> {
const token = parsePytestCollectionTokens(content, cwd);
return getTests(token.tokens);
}

interface ITestToken {
path: string;
file: string;
type: 'package' | 'module' | 'class' | 'method';
level: number;
tokens: ITestToken[];
const allTests = getTestOutputBySplittingString(content, '==DISCOVERED TESTS BEGIN==')
.split(/\r?\n/g)
.map(line => line.trim())
.map(line => line.replace(/::\(\)/g, ''))
.filter(line => line)
.map(line => splitModule(line, cwd))
.filter(line => line)
.map(line => line!);
return Array.from(groupBy(allTests, t => t.modulePath).entries())
.map(([modulePath, tests]) => ({
type: 'suite' as 'suite',
id: modulePath,
label: path.basename(modulePath),
file: modulePath,
children: toTestSuites(tests.map(t => ({ head: t.modulePath, tail: t.testPath }))),
}));
}

function getTests(tokens: ITestToken[]): Array<TestSuiteInfo | TestInfo> {
if (empty(tokens)) {
function toTestSuites(tests: Array<{ head: string, tail: string }>): Array<TestSuiteInfo | TestInfo> {
if (empty(tests)) {
return [];
}
return tokens.map(token => {
if (token.type === 'module' || token.type === 'class') {
const suite: TestSuiteInfo = {
type: 'suite',
id: token.path,
label: getLabel(token.path),
file: token.file,
children: getTests(token.tokens),
};
return [suite];
}
if (token.type === 'method') {
const test: TestInfo = {
type: 'test',
id: token.path,
label: getLabel(token.path),
};
return [test];
}
return getTests(token.tokens);
}).filter(x => x).map(x => x!).reduce((r, x) => r.concat(x), []);
const testsAndSuites = groupBy(tests, t => t.tail.includes('::'));
return (toFirstLevelTests(testsAndSuites.get(false)) as Array<TestSuiteInfo | TestInfo>)
.concat(toSuites(testsAndSuites.get(true)) as Array<TestSuiteInfo | TestInfo>);
}

function getLabel(tokenPath: string): string {
const indexOfSplit = tokenPath.lastIndexOf('::');
if (indexOfSplit < 0) {
return path.basename(tokenPath);
function toSuites(suites: Array<{ head: string, tail: string }> | undefined): TestSuiteInfo[] {
if (!suites) {
return [];
}
return tokenPath.substring(indexOfSplit + 2);
return Array.from(groupBy(suites.map(test => splitTest(test)), group => group.head).entries())
.map(([suite, suiteTests]) => ({
type: 'suite' as 'suite',
id: suite,
label: suiteTests[0].name,
file: suite,
children: toTestSuites(suiteTests),
}));
}

function parseLine(line: string, level: number, parent: ITestToken): ITestToken | undefined {
const nameBeginIndex = line.indexOf('\'');
const nameEndIndex = line.lastIndexOf('\'');
if (nameBeginIndex < 0 || nameEndIndex < 0 || (nameEndIndex - nameBeginIndex) < 1) {
return undefined;
}

const name = line.substring(nameBeginIndex + 1, nameEndIndex);
if (line.startsWith('<Package \'')) {
return {
path: name,
file: name,
type: 'package',
level,
tokens: [],
};
}

if (line.startsWith('<Module \'')) {
const modulePath = path.resolve(parent.path, name);
return {
path: modulePath,
file: modulePath,
type: 'module',
level,
tokens: [],
};
}

if (line.startsWith('<Class \'') ||
line.startsWith('<UnitTestCase \'') ||
line.startsWith('<DescribeBlock \'')
) {
return {
path: `${parent.path}::${name}`,
file: parent.file,
type: 'class',
level,
tokens: [],
};
}

if (line.startsWith('<TestCaseFunction \'') || line.startsWith('<Function \'')) {
return {
path: `${parent.path}::${name}`,
file: parent.file,
type: 'method',
level,
tokens: [],
};
function toFirstLevelTests(tests: Array<{ head: string, tail: string }> | undefined): TestInfo[] {
if (!tests) {
return [];
}

return undefined;
return tests.map(test => ({
id: `${test.head}::${test.tail}`,
label: test.tail,
type: 'test' as 'test',
}));
}

function parsePytestCollectionTokens(content: string, cwd: string): ITestToken {
const rootTestSuite: ITestToken = {
path: cwd,
file: cwd,
type: 'package',
level: -1,
tokens: [],
function splitTest(test: { head: string, tail: string }) {
const separatorIndex = test.tail.indexOf('::');
return {
head: `${test.head}::${test.tail.substring(0, separatorIndex)}`,
tail: test.tail.substring(separatorIndex + 2),
name: test.tail.substring(0, separatorIndex),
};
const testSuites: ITestToken[] = [rootTestSuite];
content.split(/\r?\n/g)
.forEach(line => {
const trimmedLine = line.trim();
if (!trimmedLine) {
return;
}
}

const level = line.indexOf('<');
if (level < 0) {
return;
}
while (level <= testSuites[testSuites.length - 1].level) {
testSuites.pop();
}
const parent = testSuites[testSuites.length - 1];
const token = parseLine(trimmedLine, level, parent);
if (!token) {
return;
}
parent.tokens.push(token);
testSuites.push(token);
});
return rootTestSuite;
function splitModule(testId: string, cwd: string) {
const separatorIndex = testId.indexOf('::');
if (separatorIndex < 0) {
return null;
}
return {
modulePath: path.resolve(cwd, testId.substring(0, separatorIndex)),
testPath: testId.substring(separatorIndex + 2),
};
}
13 changes: 11 additions & 2 deletions src/pytest/pytestTestRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ import { parseTestSuites } from './pytestTestCollectionParser';

export class PytestTestRunner implements ITestRunner {
private static readonly PYTEST_WRAPPER_SCRIPT = `
from __future__ import print_function

import pytest
import sys

pytest.main(sys.argv[1:])`;
class PythonTestExplorerDiscoveryOutputPlugin(object):
def pytest_collection_finish(self, session):
print('==DISCOVERED TESTS BEGIN==')
for item in session.items:
print(item.nodeid)
print('==DISCOVERED TESTS END==')

pytest.main(sys.argv[1:], plugins=[PythonTestExplorerDiscoveryOutputPlugin()])`;

constructor(
public readonly adapterId: string,
Expand All @@ -36,7 +45,7 @@ pytest.main(sys.argv[1:])`;
const output = await runScript({
pythonPath: config.pythonPath(),
script: PytestTestRunner.PYTEST_WRAPPER_SCRIPT,
args: ['--collect-only'],
args: ['--collect-only', '-qq'],
cwd: config.getCwd(),
environment: additionalEnvironment,
});
Expand Down
17 changes: 1 addition & 16 deletions src/unittest/unittestSuitParser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Base64 } from 'js-base64';
import * as path from 'path';
import { TestEvent, TestSuiteInfo } from 'vscode-test-adapter-api';
import { getTestOutputBySplittingString, groupBy } from '../utilities';

export function parseTestSuites(output: string, cwd: string): TestSuiteInfo[] {
const allTests = getTestOutputBySplittingString(output, '==DISCOVERED TESTS==')
Expand Down Expand Up @@ -51,11 +52,6 @@ function tryParseTestState(line: string): TestEvent | undefined {
};
}

function getTestOutputBySplittingString(output: string, stringToSplitWith: string): string {
const split = output.split(stringToSplitWith);
return split && split.pop() || '';
}

function toState(value: string): 'running' | 'passed' | 'failed' | 'skipped' | undefined {
switch (value) {
case 'running':
Expand All @@ -68,17 +64,6 @@ function toState(value: string): 'running' | 'passed' | 'failed' | 'skipped' | u
}
}

function groupBy<T, U>(values: T[], key: (v: T) => U) {
return values.reduce((accumulator, x) => {
if (accumulator.has(key(x))) {
accumulator.get(key(x))!.push(x);
} else {
accumulator.set(key(x), [x]);
}
return accumulator;
}, new Map<U, T[]>());
}

function splitTestId(testId: string) {
const separatorIndex = testId.lastIndexOf('.');
if (separatorIndex < 0) {
Expand Down
16 changes: 16 additions & 0 deletions src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,19 @@
export function empty<T>(x: T[]) {
return !x || !x.length;
}

export function getTestOutputBySplittingString(output: string, stringToSplitWith: string): string {
const split = output.split(stringToSplitWith);
return split && split.pop() || '';
}

export function groupBy<T, U>(values: T[], key: (v: T) => U) {
return values.reduce((accumulator, x) => {
if (accumulator.has(key(x))) {
accumulator.get(key(x))!.push(x);
} else {
accumulator.set(key(x), [x]);
}
return accumulator;
}, new Map<U, T[]>());
}
Loading