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
186 lines (170 loc) · 9.46 KB
/
Copy pathpytest.test.ts
File metadata and controls
186 lines (170 loc) · 9.46 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
186
import * as assert from 'assert';
import * as vscode from 'vscode';
import * as pytest from '../../client/unittests/pytest/main';
import * as path from 'path';
import * as configSettings from '../../client/common/configSettings';
import { initialize } from './../initialize';
import { TestsToRun, TestFile } from '../../client/unittests/common/contracts';
import { TestResultDisplay } from '../../client/unittests/display/main';
import { MockOutputChannel } from './../mockClasses';
const pythonSettings = configSettings.PythonSettings.getInstance();
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');
suite('Unit Tests (PyTest)', () => {
suiteSetup(() => initialize());
setup(() => {
rootDirectory = UNITTEST_TEST_FILES_PATH;
outChannel = new MockOutputChannel('Python Test Log');
testResultDisplay = new TestResultDisplay(outChannel);
});
teardown(() => {
outChannel.dispose();
testManager.dispose();
testResultDisplay.dispose();
});
function createTestManager(rootDir: string = rootDirectory) {
testManager = new pytest.TestManager(rootDir, outChannel);
}
let rootDirectory = UNITTEST_TEST_FILES_PATH;
let testManager: pytest.TestManager;
let testResultDisplay: TestResultDisplay;
let outChannel: vscode.OutputChannel;
test('Discover Tests (single test file)', async () => {
pythonSettings.unitTest.nosetestArgs = [
];
testManager = new pytest.TestManager(UNITTEST_SINGLE_TEST_FILE_PATH, outChannel);
const tests = await testManager.discoverTests(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.testSuits.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 () => {
pythonSettings.unitTest.pyTestArgs = [
'-k=test_'
];
createTestManager();
const tests = await testManager.discoverTests(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.testSuits.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 () => {
pythonSettings.unitTest.pyTestArgs = [
'-k=_test.py'
];
createTestManager();
const tests = await testManager.discoverTests(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.testSuits.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 () => {
pythonSettings.unitTest.pyTestArgs = [];
rootDirectory = UNITTEST_TEST_FILES_PATH_WITH_CONFIGS;
createTestManager();
const tests = await testManager.discoverTests(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.testSuits.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 () => {
pythonSettings.unitTest.pyTestArgs = [
'-k=test_'
];
createTestManager();
const results = await testManager.runTest();
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 () => {
pythonSettings.unitTest.pyTestArgs = [
'-k=test_'
];
createTestManager();
let results = await testManager.runTest()
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(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 () => {
pythonSettings.unitTest.pyTestArgs = [
'-k=test_'
];
createTestManager();
await testManager.discoverTests(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(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 () => {
pythonSettings.unitTest.pyTestArgs = [
'-k=test_'
];
createTestManager();
const tests = await testManager.discoverTests(true, true);
const testSuite: TestsToRun = { testFile: [], testFolder: [], testFunction: [], testSuite: [tests.testSuits[0].testSuite] };
const results = await testManager.runTest(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 () => {
pythonSettings.unitTest.pyTestArgs = [
'-k=test_'
];
createTestManager();
const tests = await testManager.discoverTests(true, true);
const testFn: TestsToRun = { testFile: [], testFolder: [], testFunction: [tests.testFunctions[0].testFunction], testSuite: [] };
const results = await testManager.runTest(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 () => {
pythonSettings.unitTest.unittestArgs = [
'-s=./tests',
'-p=test*.py'
];
createTestManager(unitTestTestFilesCwdPath);
const tests = await testManager.discoverTests(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.testSuits.length, 1, 'Incorrect number of test suites');
});
});