Skip to content

Unittest top-level-directory option #19398

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 4 commits into from
Jul 14, 2022
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
5 changes: 3 additions & 2 deletions pythonFiles/testing_tools/unittest_discovery.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import unittest
import inspect
import os
import sys
import traceback
import unittest

start_dir = sys.argv[1]
pattern = sys.argv[2]
top_level_dir = sys.argv[3] if len(sys.argv) >= 4 else None
sys.path.insert(0, os.getcwd())


Expand Down Expand Up @@ -38,7 +39,7 @@ def generate_test_cases(suite):

try:
loader = unittest.TestLoader()
suite = loader.discover(start_dir, pattern=pattern)
suite = loader.discover(start_dir, pattern=pattern, top_level_dir=top_level_dir)

print("start") # Don't remove this line
loader_errors = []
Expand Down
15 changes: 8 additions & 7 deletions pythonFiles/visualstudio_py_testlauncher.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
__author__ = "Microsoft Corporation <[email protected]>"
__version__ = "3.0.0.0"

import os
import sys
import json
import unittest
import os
import signal
import socket
import sys
import traceback
from types import CodeType, FunctionType
import signal
import unittest

try:
import thread
Expand Down Expand Up @@ -295,8 +294,8 @@ def main():
if opts.mixed_mode:
# For mixed-mode attach, there's no ptvsd and hence no wait_for_attach(),
# so we have to use Win32 API in a loop to do the same thing.
from ctypes import c_char, windll
from time import sleep
from ctypes import windll, c_char

while True:
if windll.kernel32.IsDebuggerPresent() != 0:
Expand Down Expand Up @@ -334,7 +333,9 @@ def main():
# Easier approach is find the test suite and use that for running
loader = unittest.TestLoader()
# opts.us will be passed in
suites = loader.discover(opts.us, pattern=os.path.basename(opts.testFile))
suites = loader.discover(
opts.us, pattern=os.path.basename(opts.testFile), top_level_dir=opts.ut
)
suite = None
tests = None
if opts.tests is None:
Expand Down
16 changes: 16 additions & 0 deletions src/client/testing/testController/unittest/arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,29 @@ export function unittestGetTestPattern(args: string[]): string {
return 'test*.py';
}

export function unittestGetTopLevelDirectory(args: string[]): string | null {
const shortValue = getOptionValues(args, '-t');
if (shortValue.length === 1) {
return shortValue[0];
}
const longValue = getOptionValues(args, '--top-level-directory');
if (longValue.length === 1) {
return longValue[0];
}
return null;
}

export function getTestRunArgs(args: string[]): string[] {
const startTestDiscoveryDirectory = unittestGetTestFolders(args)[0];
const pattern = unittestGetTestPattern(args);
const topLevelDir = unittestGetTopLevelDirectory(args);

const failFast = args.some((arg) => arg.trim() === '-f' || arg.trim() === '--failfast');
const verbosity = args.some((arg) => arg.trim().indexOf('-v') === 0) ? 2 : 1;
const testArgs = [`--us=${startTestDiscoveryDirectory}`, `--up=${pattern}`, `--uvInt=${verbosity}`];
if (topLevelDir) {
testArgs.push(`--ut=${topLevelDir}`);
}
if (failFast) {
testArgs.push('--uf');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
RawTestParent,
TestData,
} from '../common/types';
import { unittestGetTestFolders, unittestGetTestPattern } from './arguments';
import { unittestGetTestFolders, unittestGetTestPattern, unittestGetTopLevelDirectory } from './arguments';
import {
createErrorTestItem,
createWorkspaceRootTestItem,
Expand Down Expand Up @@ -130,16 +130,20 @@ export class UnittestController implements ITestFrameworkController {

const startDir = unittestGetTestFolders(options.args)[0];
const pattern = unittestGetTestPattern(options.args);
const topLevelDir = unittestGetTopLevelDirectory(options.args);
let testDir = startDir;
if (path.isAbsolute(startDir)) {
const relative = path.relative(options.cwd, startDir);
testDir = relative.length > 0 ? relative : '.';
}

const runOptionsArgs: string[] =
topLevelDir == null ? [startDir, pattern] : [startDir, pattern, topLevelDir];

const runOptions: Options = {
// unittest needs to load modules in the workspace
// isolating it breaks unittest discovery
args: unittestDiscovery([startDir, pattern]),
args: unittestDiscovery(runOptionsArgs),
cwd: options.cwd,
workspaceFolder: options.workspaceFolder,
token: options.token,
Expand Down