Skip to content

Commit

Permalink
Merge pull request #65 from kondratyev-nv/feature/fix_unittest_argume…
Browse files Browse the repository at this point in the history
…nt_passing

Pass unittest arguments as script arguments
  • Loading branch information
Nikolay Kondratyev authored Feb 14, 2019
2 parents 7eacfab + fd5292c commit 10af628
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
18 changes: 9 additions & 9 deletions src/unittest/unittestScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

export const TEST_RESULT_PREFIX = 'TEST_EXECUTION_RESULT';

export function unittestHelperScript(configuration: { startDirectory: string, pattern: string }) {
return `
export const UNITTEST_TEST_RUNNER_SCRIPT = `
from __future__ import print_function
from unittest import TextTestRunner, TextTestResult, TestLoader, TestSuite, defaultTestLoader as loader
import sys
Expand Down Expand Up @@ -60,8 +59,8 @@ def get_tests(suite):
return [suite]
def discover_tests():
return get_tests(loader.discover("${configuration.startDirectory}", pattern="${configuration.pattern}"))
def discover_tests(start_directory, pattern):
return get_tests(loader.discover(start_directory, pattern=pattern))
def filter_by_test_ids(tests, test_ids):
Expand All @@ -75,9 +74,9 @@ def write_test_state(state, result):
print("{}:{}:{}:{}".format(TEST_RESULT_PREFIX, state, result[0].id(), message))
def run_tests(test_names):
def run_tests(start_directory, pattern, test_names):
runner = TextTestRunnerWithSingleResult()
tests = [TestSuite([test]) for test in filter_by_test_ids(discover_tests(), test_names)]
tests = [TestSuite([test]) for test in filter_by_test_ids(discover_tests(start_directory, pattern), test_names)]
for test in tests:
result = runner.run(test)
for r in result.skipped:
Expand All @@ -91,13 +90,14 @@ def run_tests(test_names):
action = sys.argv[1]
start_directory = sys.argv[2]
pattern = sys.argv[3]
if action == "discover":
tests = discover_tests()
tests = discover_tests(start_directory, pattern)
print("==DISCOVERED TESTS==")
for test in tests:
print(test.id())
elif action == "run":
run_tests(sys.argv[2:])
run_tests(start_directory, pattern, sys.argv[4:])
else:
raise ValueError("invalid command: should be discover or run")`;
}
12 changes: 7 additions & 5 deletions src/unittest/unittestTestRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ILogger } from '../logging/logger';
import { IProcessExecution, runScript } from '../pythonRunner';
import { IDebugConfiguration, ITestRunner } from '../testRunner';
import { empty, ensureDifferentLabels } from '../utilities';
import { unittestHelperScript } from './unittestScripts';
import { UNITTEST_TEST_RUNNER_SCRIPT } from './unittestScripts';
import { parseTestStates, parseTestSuites } from './unittestSuitParser';

export class UnittestTestRunner implements ITestRunner {
Expand Down Expand Up @@ -49,8 +49,8 @@ export class UnittestTestRunner implements ITestRunner {

const result = await runScript({
pythonPath: config.pythonPath(),
script: unittestHelperScript(unittestArguments),
args: ['discover'],
script: UNITTEST_TEST_RUNNER_SCRIPT,
args: ['discover', unittestArguments.startDirectory, unittestArguments.pattern],
cwd: config.getCwd(),
environment: additionalEnvironment,
}).complete();
Expand All @@ -77,9 +77,11 @@ export class UnittestTestRunner implements ITestRunner {
const additionalEnvironment = await EnvironmentVariablesLoader.load(config.envFile(), this.logger);
const testExecution = runScript({
pythonPath: config.pythonPath(),
script: unittestHelperScript(unittestArguments),
script: UNITTEST_TEST_RUNNER_SCRIPT,
cwd: config.getCwd(),
args: test !== this.adapterId ? ['run', test] : ['run'],
args: test !== this.adapterId ?
['run', unittestArguments.startDirectory, unittestArguments.pattern, test] :
['run', unittestArguments.startDirectory, unittestArguments.pattern],
environment: additionalEnvironment,
});
this.testExecutions.set(test, testExecution);
Expand Down

0 comments on commit 10af628

Please sign in to comment.