GeezerVison is showing github.com — Unittest top-level-directory option (#19398) · chien110234/pythonVSCode@4ecf02b
File treeExpand file treeCollapse file tree Open diff view settings src/client/testing/testController/unittest
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pythonFiles/testing_tools/unittest_discovery.py +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import unittest | 2 | 1 | import inspect | 3 | 2 | import os | 4 | 3 | import sys | 5 | 4 | import traceback | | 5 | +import unittest | 6 | 6 |
| 7 | 7 | start_dir = sys.argv[1] | 8 | 8 | pattern = sys.argv[2] | | 9 | +top_level_dir = sys.argv[3] if len(sys.argv) >= 4 else None | 9 | 10 | sys.path.insert(0, os.getcwd()) | 10 | 11 |
| 11 | 12 |
| @@ -38,7 +39,7 @@ def generate_test_cases(suite): | 38 | 39 |
| 39 | 40 | try: | 40 | 41 | loader = unittest.TestLoader() | 41 | | - suite = loader.discover(start_dir, pattern=pattern) | | 42 | + suite = loader.discover(start_dir, pattern=pattern, top_level_dir=top_level_dir) | 42 | 43 |
| 43 | 44 | print("start") # Don't remove this line | 44 | 45 | loader_errors = [] | |
Collapse file Copy file name to clipboardExpand all lines: pythonFiles/visualstudio_py_testlauncher.py +8-7Lines changed: 8 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 17 | 17 | | 18 | 18 | __version__ = "3.0.0.0" | 19 | 19 |
| 20 | | -import os | 21 | | -import sys | 22 | 20 | import json | 23 | | -import unittest | | 21 | +import os | | 22 | +import signal | 24 | 23 | import socket | | 24 | +import sys | 25 | 25 | import traceback | 26 | | -from types import CodeType, FunctionType | 27 | | -import signal | | 26 | +import unittest | 28 | 27 |
| 29 | 28 | try: | 30 | 29 | import thread | @@ -295,8 +294,8 @@ def main(): | 295 | 294 | if opts.mixed_mode: | 296 | 295 | # For mixed-mode attach, there's no ptvsd and hence no wait_for_attach(), | 297 | 296 | # so we have to use Win32 API in a loop to do the same thing. | | 297 | + from ctypes import c_char, windll | 298 | 298 | from time import sleep | 299 | | - from ctypes import windll, c_char | 300 | 299 |
| 301 | 300 | while True: | 302 | 301 | if windll.kernel32.IsDebuggerPresent() != 0: | @@ -334,7 +333,9 @@ def main(): | 334 | 333 | # Easier approach is find the test suite and use that for running | 335 | 334 | loader = unittest.TestLoader() | 336 | 335 | # opts.us will be passed in | 337 | | - suites = loader.discover(opts.us, pattern=os.path.basename(opts.testFile)) | | 336 | + suites = loader.discover( | | 337 | + opts.us, pattern=os.path.basename(opts.testFile), top_level_dir=opts.ut | | 338 | + ) | 338 | 339 | suite = None | 339 | 340 | tests = None | 340 | 341 | if opts.tests is None: | |
Collapse file Copy file name to clipboardExpand all lines: src/client/testing/testController/unittest/arguments.ts +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -73,13 +73,29 @@ export function unittestGetTestPattern(args: string[]): string { | 73 | 73 | return 'test*.py'; | 74 | 74 | } | 75 | 75 |
| | 76 | +export function unittestGetTopLevelDirectory(args: string[]): string | null { | | 77 | + const shortValue = getOptionValues(args, '-t'); | | 78 | + if (shortValue.length === 1) { | | 79 | + return shortValue[0]; | | 80 | + } | | 81 | + const longValue = getOptionValues(args, '--top-level-directory'); | | 82 | + if (longValue.length === 1) { | | 83 | + return longValue[0]; | | 84 | + } | | 85 | + return null; | | 86 | +} | | 87 | + | 76 | 88 | export function getTestRunArgs(args: string[]): string[] { | 77 | 89 | const startTestDiscoveryDirectory = unittestGetTestFolders(args)[0]; | 78 | 90 | const pattern = unittestGetTestPattern(args); | | 91 | + const topLevelDir = unittestGetTopLevelDirectory(args); | 79 | 92 |
| 80 | 93 | const failFast = args.some((arg) => arg.trim() === '-f' || arg.trim() === '--failfast'); | 81 | 94 | const verbosity = args.some((arg) => arg.trim().indexOf('-v') === 0) ? 2 : 1; | 82 | 95 | const testArgs = [`--us=${startTestDiscoveryDirectory}`, `--up=${pattern}`, `--uvInt=${verbosity}`]; | | 96 | + if (topLevelDir) { | | 97 | + testArgs.push(`--ut=${topLevelDir}`); | | 98 | + } | 83 | 99 | if (failFast) { | 84 | 100 | testArgs.push('--uf'); | 85 | 101 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/client/testing/testController/unittest/unittestController.ts +6-2Lines changed: 6 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,7 +19,7 @@ import { | 19 | 19 | RawTestParent, | 20 | 20 | TestData, | 21 | 21 | } from '../common/types'; | 22 | | -import { unittestGetTestFolders, unittestGetTestPattern } from './arguments'; | | 22 | +import { unittestGetTestFolders, unittestGetTestPattern, unittestGetTopLevelDirectory } from './arguments'; | 23 | 23 | import { | 24 | 24 | createErrorTestItem, | 25 | 25 | createWorkspaceRootTestItem, | @@ -130,16 +130,20 @@ export class UnittestController implements ITestFrameworkController { | 130 | 130 |
| 131 | 131 | const startDir = unittestGetTestFolders(options.args)[0]; | 132 | 132 | const pattern = unittestGetTestPattern(options.args); | | 133 | + const topLevelDir = unittestGetTopLevelDirectory(options.args); | 133 | 134 | let testDir = startDir; | 134 | 135 | if (path.isAbsolute(startDir)) { | 135 | 136 | const relative = path.relative(options.cwd, startDir); | 136 | 137 | testDir = relative.length > 0 ? relative : '.'; | 137 | 138 | } | 138 | 139 |
| | 140 | + const runOptionsArgs: string[] = | | 141 | + topLevelDir == null ? [startDir, pattern] : [startDir, pattern, topLevelDir]; | | 142 | + | 139 | 143 | const runOptions: Options = { | 140 | 144 | // unittest needs to load modules in the workspace | 141 | 145 | // isolating it breaks unittest discovery | 142 | | - args: unittestDiscovery([startDir, pattern]), | | 146 | + args: unittestDiscovery(runOptionsArgs), | 143 | 147 | cwd: options.cwd, | 144 | 148 | workspaceFolder: options.workspaceFolder, | 145 | 149 | token: options.token, | |
|