forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
baseTestManager.unit.test.ts
169 lines (147 loc) · 10.5 KB
/
baseTestManager.unit.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
// tslint:disable:no-any
import * as sinon from 'sinon';
import { anything, instance, mock, verify, when } from 'ts-mockito';
import { Disposable, OutputChannel, Uri } from 'vscode';
import { CommandManager } from '../../../../client/common/application/commandManager';
import { ICommandManager, IWorkspaceService } from '../../../../client/common/application/types';
import { WorkspaceService } from '../../../../client/common/application/workspace';
import { PythonSettings } from '../../../../client/common/configSettings';
import { ConfigurationService } from '../../../../client/common/configuration/service';
import { ModuleNotInstalledError } from '../../../../client/common/errors/moduleNotInstalledError';
import { ProductInstaller } from '../../../../client/common/installer/productInstaller';
import { IConfigurationService, IDisposableRegistry, IInstaller, IOutputChannel, IPythonSettings } from '../../../../client/common/types';
import { ServiceContainer } from '../../../../client/ioc/container';
import { IServiceContainer } from '../../../../client/ioc/types';
import { CommandSource, TEST_OUTPUT_CHANNEL } from '../../../../client/testing/common/constants';
import { TestCollectionStorageService } from '../../../../client/testing/common/services/storageService';
import { TestResultsService } from '../../../../client/testing/common/services/testResultsService';
import { TestsStatusUpdaterService } from '../../../../client/testing/common/services/testsStatusService';
import { UnitTestDiagnosticService } from '../../../../client/testing/common/services/unitTestDiagnosticService';
import { TestsHelper } from '../../../../client/testing/common/testUtils';
import { ITestCollectionStorageService, ITestDiscoveryService, ITestManager, ITestMessageService, ITestResultsService, ITestsHelper, ITestsStatusUpdaterService } from '../../../../client/testing/common/types';
import { TestManager as NoseTestManager } from '../../../../client/testing/nosetest/main';
import { TestManager as PyTestTestManager } from '../../../../client/testing/pytest/main';
import { ArgumentsService } from '../../../../client/testing/pytest/services/argsService';
import { TestDiscoveryService } from '../../../../client/testing/pytest/services/discoveryService';
import { TestMessageService } from '../../../../client/testing/pytest/services/testMessageService';
import { IArgumentsService, ITestDiagnosticService, ITestManagerRunner } from '../../../../client/testing/types';
import { TestManager as UnitTestTestManager } from '../../../../client/testing/unittest/main';
import { TestManagerRunner } from '../../../../client/testing/unittest/runner';
import { noop } from '../../../core';
import { MockOutputChannel } from '../../../mockClasses';
// tslint:disable: max-func-body-length
suite('Unit Tests - Base Test Manager', () => {
[
{ name: 'nose', class: NoseTestManager },
{ name: 'pytest', class: PyTestTestManager },
{ name: 'unittest', class: UnitTestTestManager }
].forEach(item => {
suite(item.name, () => {
let testManager: ITestManager;
const workspaceFolder = Uri.file(__dirname);
let serviceContainer: IServiceContainer;
let configService: IConfigurationService;
let settings: IPythonSettings;
let outputChannel: IOutputChannel;
let storageService: ITestCollectionStorageService;
let resultsService: ITestResultsService;
let workspaceService: IWorkspaceService;
let diagnosticService: ITestDiagnosticService;
let statusUpdater: ITestsStatusUpdaterService;
let commandManager: ICommandManager;
let testDiscoveryService: ITestDiscoveryService;
let installer: IInstaller;
const sandbox = sinon.createSandbox();
suiteTeardown(() => sandbox.restore());
setup(() => {
serviceContainer = mock(ServiceContainer);
settings = mock(PythonSettings);
configService = mock(ConfigurationService);
outputChannel = mock(MockOutputChannel);
storageService = mock(TestCollectionStorageService);
resultsService = mock(TestResultsService);
workspaceService = mock(WorkspaceService);
diagnosticService = mock(UnitTestDiagnosticService);
statusUpdater = mock(TestsStatusUpdaterService);
commandManager = mock(CommandManager);
testDiscoveryService = mock(TestDiscoveryService);
installer = mock(ProductInstaller);
const argsService = mock(ArgumentsService);
const testsHelper = mock(TestsHelper);
const runner = mock(TestManagerRunner);
const messageService = mock(TestMessageService);
when(serviceContainer.get<IConfigurationService>(IConfigurationService)).thenReturn(instance(configService));
when(serviceContainer.get<Disposable[]>(IDisposableRegistry)).thenReturn([]);
when(serviceContainer.get<OutputChannel>(IOutputChannel, TEST_OUTPUT_CHANNEL)).thenReturn(instance(outputChannel));
when(serviceContainer.get<ITestCollectionStorageService>(ITestCollectionStorageService)).thenReturn(instance(storageService));
when(serviceContainer.get<ITestResultsService>(ITestResultsService)).thenReturn(instance(resultsService));
when(serviceContainer.get<IWorkspaceService>(IWorkspaceService)).thenReturn(instance(workspaceService));
when(serviceContainer.get<ITestDiagnosticService>(ITestDiagnosticService)).thenReturn(instance(diagnosticService));
when(serviceContainer.get<ITestsStatusUpdaterService>(ITestsStatusUpdaterService)).thenReturn(instance(statusUpdater));
when(serviceContainer.get<ICommandManager>(ICommandManager)).thenReturn(instance(commandManager));
when(serviceContainer.get<IArgumentsService>(IArgumentsService, anything())).thenReturn(instance(argsService));
when(serviceContainer.get<ITestsHelper>(ITestsHelper)).thenReturn(instance(testsHelper));
when(serviceContainer.get<ITestManagerRunner>(ITestManagerRunner, anything())).thenReturn(instance(runner));
when(serviceContainer.get<ITestMessageService>(ITestMessageService, anything())).thenReturn(instance(messageService));
when(serviceContainer.get<ITestDiscoveryService>(ITestDiscoveryService, anything())).thenReturn(instance(testDiscoveryService));
when(serviceContainer.get<IInstaller>(IInstaller)).thenReturn(instance(installer));
when(configService.getSettings(anything())).thenReturn(instance(settings));
when(commandManager.executeCommand(anything(), anything(), anything())).thenResolve();
sandbox.restore();
sandbox.stub(item.class.prototype, 'getDiscoveryOptions').callsFake(() => ({} as any));
testManager = new item.class(workspaceFolder, workspaceFolder.fsPath, instance(serviceContainer));
});
test('Discovering tests should display test manager', async () => {
// We don't care about failures in running code
// Just test our expectations, ignore everything else.
await testManager.discoverTests(CommandSource.auto, true, true, true).catch(noop);
verify(commandManager.executeCommand('setContext', 'testsDiscovered', true)).once();
});
test('When failing to discover tests prompt to install test framework', async function () {
if (item.name === 'unittest') {
// tslint:disable-next-line: no-invalid-this
return this.skip();
}
when(testDiscoveryService.discoverTests(anything())).thenReject(new ModuleNotInstalledError('Kaboom'));
when(installer.isInstalled(anything(), anything())).thenResolve(false);
when(installer.promptToInstall(anything(), anything())).thenResolve();
// We don't care about failures in running code
// Just test our expectations, ignore everything else.
await testManager.discoverTests(CommandSource.ui, true, false, true).catch(noop);
verify(installer.isInstalled(anything(), anything())).once();
verify(installer.promptToInstall(anything(), anything())).once();
});
test('When failing to discover tests do not prompt to install test framework', async function () {
if (item.name === 'unittest') {
// tslint:disable-next-line: no-invalid-this
return this.skip();
}
when(testDiscoveryService.discoverTests(anything())).thenReject(new Error('Kaboom'));
when(installer.isInstalled(anything(), anything())).thenResolve(false);
when(installer.promptToInstall(anything(), anything())).thenResolve();
// We don't care about failures in running code
// Just test our expectations, ignore everything else.
await testManager.discoverTests(CommandSource.ui, true, false, true).catch(noop);
verify(installer.isInstalled(anything(), anything())).never();
verify(installer.promptToInstall(anything(), anything())).never();
});
test('When failing to discover tests do not prompt to install test framework if installed', async function () {
if (item.name === 'unittest') {
// tslint:disable-next-line: no-invalid-this
return this.skip();
}
when(testDiscoveryService.discoverTests(anything())).thenReject(new ModuleNotInstalledError('Kaboom'));
when(installer.isInstalled(anything(), anything())).thenResolve(true);
when(installer.promptToInstall(anything(), anything())).thenResolve();
// We don't care about failures in running code
// Just test our expectations, ignore everything else.
await testManager.discoverTests(CommandSource.ui, true, false, true).catch(noop);
verify(installer.isInstalled(anything(), anything())).once();
verify(installer.promptToInstall(anything(), anything())).never();
});
});
});
});