Skip to content

Commit f96e762

Browse files
authored
Make sure user modules can be imported (#11353)
* Make sure user modules can be imported * Update tests
1 parent 1415d81 commit f96e762

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

news/2 Fixes/11264.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow user modules import when discovering tests.

src/client/testing/unittest/services/discoveryService.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ export class TestDiscoveryService implements ITestDiscoveryService {
2929
const pythonScript = this.getDiscoveryScript(options);
3030
const unitTestOptions = this.translateOptions(options);
3131
const runOptions: Options = {
32-
args: internalPython.execCode(pythonScript),
32+
// unittest needs to load modules in the workspace
33+
// isolating it breaks unittest discovery
34+
args: internalPython.execCode(pythonScript, false),
3335
cwd: options.cwd,
3436
workspaceFolder: options.workspaceFolder,
3537
token: options.token,

src/test/testing/unittest/unittest.discovery.unit.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ suite('Unit Tests - Unittest - Discovery', () => {
7171
.setup((r) => r.run(typeMoq.It.isValue(UNITTEST_PROVIDER), typeMoq.It.isAny()))
7272
.callback((_, opts: Options) => {
7373
expect(opts.args).to.include('-c');
74-
expect(opts.args[2]).to.contain(dir);
75-
expect(opts.args[2]).to.not.contain('loader.discover("."');
74+
expect(opts.args[1]).to.contain(dir);
75+
expect(opts.args[1]).to.not.contain('loader.discover("."');
7676
})
7777
.returns(() => Promise.resolve(runOutput))
7878
.verifiable(typeMoq.Times.once());
@@ -117,8 +117,8 @@ suite('Unit Tests - Unittest - Discovery', () => {
117117
.setup((r) => r.run(typeMoq.It.isValue(UNITTEST_PROVIDER), typeMoq.It.isAny()))
118118
.callback((_, opts: Options) => {
119119
expect(opts.args).to.include('-c');
120-
expect(opts.args[2]).to.contain(dir);
121-
expect(opts.args[2]).to.not.contain('loader.discover("."');
120+
expect(opts.args[1]).to.contain(dir);
121+
expect(opts.args[1]).to.not.contain('loader.discover("."');
122122
})
123123
.returns(() => Promise.resolve(runOutput))
124124
.verifiable(typeMoq.Times.once());
@@ -163,8 +163,8 @@ suite('Unit Tests - Unittest - Discovery', () => {
163163
.setup((r) => r.run(typeMoq.It.isValue(UNITTEST_PROVIDER), typeMoq.It.isAny()))
164164
.callback((_, opts: Options) => {
165165
expect(opts.args).to.include('-c');
166-
expect(opts.args[2]).to.not.contain(dir);
167-
expect(opts.args[2]).to.contain('loader.discover("."');
166+
expect(opts.args[1]).to.not.contain(dir);
167+
expect(opts.args[1]).to.contain('loader.discover("."');
168168
})
169169
.returns(() => Promise.resolve(runOutput))
170170
.verifiable(typeMoq.Times.once());
@@ -205,8 +205,8 @@ suite('Unit Tests - Unittest - Discovery', () => {
205205
.setup((r) => r.run(typeMoq.It.isValue(UNITTEST_PROVIDER), typeMoq.It.isAny()))
206206
.callback((_, opts: Options) => {
207207
expect(opts.args).to.include('-c');
208-
expect(opts.args[2]).to.contain(pattern);
209-
expect(opts.args[2]).to.not.contain('test*.py');
208+
expect(opts.args[1]).to.contain(pattern);
209+
expect(opts.args[1]).to.not.contain('test*.py');
210210
})
211211
.returns(() => Promise.resolve(runOutput))
212212
.verifiable(typeMoq.Times.once());
@@ -251,8 +251,8 @@ suite('Unit Tests - Unittest - Discovery', () => {
251251
.setup((r) => r.run(typeMoq.It.isValue(UNITTEST_PROVIDER), typeMoq.It.isAny()))
252252
.callback((_, opts: Options) => {
253253
expect(opts.args).to.include('-c');
254-
expect(opts.args[2]).to.contain(pattern);
255-
expect(opts.args[2]).to.not.contain('test*.py');
254+
expect(opts.args[1]).to.contain(pattern);
255+
expect(opts.args[1]).to.not.contain('test*.py');
256256
})
257257
.returns(() => Promise.resolve(runOutput))
258258
.verifiable(typeMoq.Times.once());
@@ -297,8 +297,8 @@ suite('Unit Tests - Unittest - Discovery', () => {
297297
.setup((r) => r.run(typeMoq.It.isValue(UNITTEST_PROVIDER), typeMoq.It.isAny()))
298298
.callback((_, opts: Options) => {
299299
expect(opts.args).to.include('-c');
300-
expect(opts.args[2]).to.not.contain(pattern);
301-
expect(opts.args[2]).to.contain('test*.py');
300+
expect(opts.args[1]).to.not.contain(pattern);
301+
expect(opts.args[1]).to.contain('test*.py');
302302
})
303303
.returns(() => Promise.resolve(runOutput))
304304
.verifiable(typeMoq.Times.once());

src/test/testing/unittest/unittest.run.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,10 @@ suite('Unit Tests - unittest - run with mocked process output', () => {
145145
.create()) as MockProcessService;
146146
procService.onExecObservable((_file, args, _options, callback) => {
147147
if (
148-
//args[0] is pyvsc-run-isolated.py.
149148
args.length > 1 &&
150-
args[1] === '-c' &&
151-
args[2].includes('import unittest') &&
152-
args[2].includes('loader = unittest.TestLoader()')
149+
args[0] === '-c' &&
150+
args[1].includes('import unittest') &&
151+
args[1].includes('loader = unittest.TestLoader()')
153152
) {
154153
callback({
155154
// Ensure any spaces added during code formatting or the like are removed

0 commit comments

Comments
 (0)