forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpytest.test.ts
More file actions
185 lines (171 loc) · 11.2 KB
/
pytest.test.ts
File metadata and controls
185 lines (171 loc) · 11.2 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import * as assert from 'assert';
import * as path from 'path';
import * as vscode from 'vscode';
import { CommandSource } from '../../client/unittests/common/constants';
import { TestCollectionStorageService } from '../../client/unittests/common/storageService';
import { TestResultsService } from '../../client/unittests/common/testResultsService';
import { TestsHelper } from '../../client/unittests/common/testUtils';
import { ITestCollectionStorageService, ITestResultsService, ITestsHelper, TestFile, TestsToRun } from '../../client/unittests/common/types';
import { TestResultDisplay } from '../../client/unittests/display/main';
import * as pytest from '../../client/unittests/pytest/main';
import { rootWorkspaceUri, updateSetting } from '../common';
import { initialize, initializeTest, IS_MULTI_ROOT_TEST } from './../initialize';
import { MockOutputChannel } from './../mockClasses';
import { MockDebugLauncher } from './mocks';
const UNITTEST_TEST_FILES_PATH = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'testFiles', 'standard');
const UNITTEST_SINGLE_TEST_FILE_PATH = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'testFiles', 'single');
const UNITTEST_TEST_FILES_PATH_WITH_CONFIGS = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'testFiles', 'unitestsWithConfigs');
const unitTestTestFilesCwdPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'testFiles', 'cwd', 'src');
// tslint:disable-next-line:max-func-body-length
suite('Unit Tests (PyTest)', () => {
let rootDirectory = UNITTEST_TEST_FILES_PATH;
let testManager: pytest.TestManager;
let testResultDisplay: TestResultDisplay;
let outChannel: vscode.OutputChannel;
let storageService: ITestCollectionStorageService;
let resultsService: ITestResultsService;
let testsHelper: ITestsHelper;
const configTarget = IS_MULTI_ROOT_TEST ? vscode.ConfigurationTarget.WorkspaceFolder : vscode.ConfigurationTarget.Workspace;
suiteSetup(async () => {
await initialize();
await updateSetting('unitTest.pyTestArgs', [], rootWorkspaceUri, configTarget);
});
setup(async () => {
rootDirectory = UNITTEST_TEST_FILES_PATH;
outChannel = new MockOutputChannel('Python Test Log');
testResultDisplay = new TestResultDisplay(outChannel);
await initializeTest();
});
teardown(async () => {
outChannel.dispose();
testManager.dispose();
testResultDisplay.dispose();
await updateSetting('unitTest.pyTestArgs', [], rootWorkspaceUri, configTarget);
});
function createTestManager(rootDir: string = rootDirectory) {
storageService = new TestCollectionStorageService();
resultsService = new TestResultsService();
testsHelper = new TestsHelper();
testManager = new pytest.TestManager(rootDir, outChannel, storageService, resultsService, testsHelper, new MockDebugLauncher());
}
test('Discover Tests (single test file)', async () => {
storageService = new TestCollectionStorageService();
resultsService = new TestResultsService();
testsHelper = new TestsHelper();
testManager = new pytest.TestManager(UNITTEST_SINGLE_TEST_FILE_PATH, outChannel, storageService, resultsService, testsHelper, new MockDebugLauncher());
const tests = await testManager.discoverTests(CommandSource.ui, true, true);
assert.equal(tests.testFiles.length, 2, 'Incorrect number of test files');
assert.equal(tests.testFunctions.length, 6, 'Incorrect number of test functions');
assert.equal(tests.testSuites.length, 2, 'Incorrect number of test suites');
assert.equal(tests.testFiles.some(t => t.name === 'tests/test_one.py' && t.nameToRun === t.name), true, 'Test File not found');
assert.equal(tests.testFiles.some(t => t.name === 'test_root.py' && t.nameToRun === t.name), true, 'Test File not found');
});
test('Discover Tests (pattern = test_)', async () => {
await updateSetting('unitTest.pyTestArgs', ['-k=test_'], rootWorkspaceUri, configTarget);
createTestManager();
const tests = await testManager.discoverTests(CommandSource.ui, true, true);
assert.equal(tests.testFiles.length, 6, 'Incorrect number of test files');
assert.equal(tests.testFunctions.length, 29, 'Incorrect number of test functions');
assert.equal(tests.testSuites.length, 8, 'Incorrect number of test suites');
assert.equal(tests.testFiles.some(t => t.name === 'tests/test_unittest_one.py' && t.nameToRun === t.name), true, 'Test File not found');
assert.equal(tests.testFiles.some(t => t.name === 'tests/test_unittest_two.py' && t.nameToRun === t.name), true, 'Test File not found');
assert.equal(tests.testFiles.some(t => t.name === 'tests/unittest_three_test.py' && t.nameToRun === t.name), true, 'Test File not found');
assert.equal(tests.testFiles.some(t => t.name === 'tests/test_pytest.py' && t.nameToRun === t.name), true, 'Test File not found');
assert.equal(tests.testFiles.some(t => t.name === 'tests/test_another_pytest.py' && t.nameToRun === t.name), true, 'Test File not found');
assert.equal(tests.testFiles.some(t => t.name === 'test_root.py' && t.nameToRun === t.name), true, 'Test File not found');
});
test('Discover Tests (pattern = _test)', async () => {
await updateSetting('unitTest.pyTestArgs', ['-k=_test.py'], rootWorkspaceUri, configTarget);
createTestManager();
const tests = await testManager.discoverTests(CommandSource.ui, true, true);
assert.equal(tests.testFiles.length, 1, 'Incorrect number of test files');
assert.equal(tests.testFunctions.length, 2, 'Incorrect number of test functions');
assert.equal(tests.testSuites.length, 1, 'Incorrect number of test suites');
assert.equal(tests.testFiles.some(t => t.name === 'tests/unittest_three_test.py' && t.nameToRun === t.name), true, 'Test File not found');
});
test('Discover Tests (with config)', async () => {
await updateSetting('unitTest.pyTestArgs', [], rootWorkspaceUri, configTarget);
rootDirectory = UNITTEST_TEST_FILES_PATH_WITH_CONFIGS;
createTestManager();
const tests = await testManager.discoverTests(CommandSource.ui, true, true);
assert.equal(tests.testFiles.length, 2, 'Incorrect number of test files');
assert.equal(tests.testFunctions.length, 14, 'Incorrect number of test functions');
assert.equal(tests.testSuites.length, 4, 'Incorrect number of test suites');
assert.equal(tests.testFiles.some(t => t.name === 'other/test_unittest_one.py' && t.nameToRun === t.name), true, 'Test File not found');
assert.equal(tests.testFiles.some(t => t.name === 'other/test_pytest.py' && t.nameToRun === t.name), true, 'Test File not found');
});
test('Run Tests', async () => {
await updateSetting('unitTest.pyTestArgs', ['-k=test_'], rootWorkspaceUri, configTarget);
createTestManager();
const results = await testManager.runTest(CommandSource.ui);
assert.equal(results.summary.errors, 0, 'Errors');
assert.equal(results.summary.failures, 9, 'Failures');
assert.equal(results.summary.passed, 17, 'Passed');
assert.equal(results.summary.skipped, 3, 'skipped');
});
test('Run Failed Tests', async () => {
await updateSetting('unitTest.pyTestArgs', ['-k=test_'], rootWorkspaceUri, configTarget);
createTestManager();
let results = await testManager.runTest(CommandSource.ui);
assert.equal(results.summary.errors, 0, 'Errors');
assert.equal(results.summary.failures, 9, 'Failures');
assert.equal(results.summary.passed, 17, 'Passed');
assert.equal(results.summary.skipped, 3, 'skipped');
results = await testManager.runTest(CommandSource.ui, undefined, true);
assert.equal(results.summary.errors, 0, 'Failed Errors');
assert.equal(results.summary.failures, 9, 'Failed Failures');
assert.equal(results.summary.passed, 0, 'Failed Passed');
assert.equal(results.summary.skipped, 0, 'Failed skipped');
});
test('Run Specific Test File', async () => {
await updateSetting('unitTest.pyTestArgs', ['-k=test_'], rootWorkspaceUri, configTarget);
createTestManager();
await testManager.discoverTests(CommandSource.ui, true, true);
const testFile: TestFile = {
fullPath: path.join(rootDirectory, 'tests', 'test_another_pytest.py'),
name: 'tests/test_another_pytest.py',
nameToRun: 'tests/test_another_pytest.py',
xmlName: 'tests/test_another_pytest.py',
functions: [],
suites: [],
time: 0
};
const testFileToRun: TestsToRun = { testFile: [testFile], testFolder: [], testFunction: [], testSuite: [] };
const results = await testManager.runTest(CommandSource.ui, testFileToRun);
assert.equal(results.summary.errors, 0, 'Errors');
assert.equal(results.summary.failures, 1, 'Failures');
assert.equal(results.summary.passed, 3, 'Passed');
assert.equal(results.summary.skipped, 0, 'skipped');
});
test('Run Specific Test Suite', async () => {
await updateSetting('unitTest.pyTestArgs', ['-k=test_'], rootWorkspaceUri, configTarget);
createTestManager();
const tests = await testManager.discoverTests(CommandSource.ui, true, true);
const testSuite: TestsToRun = { testFile: [], testFolder: [], testFunction: [], testSuite: [tests.testSuites[0].testSuite] };
const results = await testManager.runTest(CommandSource.ui, testSuite);
assert.equal(results.summary.errors, 0, 'Errors');
assert.equal(results.summary.failures, 1, 'Failures');
assert.equal(results.summary.passed, 1, 'Passed');
assert.equal(results.summary.skipped, 1, 'skipped');
});
test('Run Specific Test Function', async () => {
await updateSetting('unitTest.pyTestArgs', ['-k=test_'], rootWorkspaceUri, configTarget);
createTestManager();
const tests = await testManager.discoverTests(CommandSource.ui, true, true);
const testFn: TestsToRun = { testFile: [], testFolder: [], testFunction: [tests.testFunctions[0].testFunction], testSuite: [] };
const results = await testManager.runTest(CommandSource.ui, testFn);
assert.equal(results.summary.errors, 0, 'Errors');
assert.equal(results.summary.failures, 1, 'Failures');
assert.equal(results.summary.passed, 0, 'Passed');
assert.equal(results.summary.skipped, 0, 'skipped');
});
test('Setting cwd should return tests', async () => {
await updateSetting('unitTest.pyTestArgs', ['-k=test_'], rootWorkspaceUri, configTarget);
createTestManager(unitTestTestFilesCwdPath);
const tests = await testManager.discoverTests(CommandSource.ui, true, true);
assert.equal(tests.testFiles.length, 1, 'Incorrect number of test files');
assert.equal(tests.testFolders.length, 1, 'Incorrect number of test folders');
assert.equal(tests.testFunctions.length, 1, 'Incorrect number of test functions');
assert.equal(tests.testSuites.length, 1, 'Incorrect number of test suites');
});
});