Skip to content

Commit 8394303

Browse files
committed
unittest tests
1 parent 5b9a488 commit 8394303

4 files changed

Lines changed: 173 additions & 0 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//
2+
// Note: This example test is leveraging the Mocha test framework.
3+
// Please refer to their documentation on https://mochajs.org/ for help.
4+
//
5+
// Place this right on top
6+
import { initialize } from './initialize';
7+
8+
// The module \'assert\' provides assertion methods from node
9+
import * as assert from 'assert';
10+
11+
// You can import and use all API from the \'vscode\' module
12+
// as well as import your extension to test it
13+
import * as vscode from 'vscode';
14+
import { Tests, TestsToRun, TestFolder, TestFile, TestStatus, TestSuite, TestFunction, FlattenedTestFunction, CANCELLATION_REASON } from '../client/unittests/common/contracts';
15+
import * as nosetests from '../client/unittests/nosetest/main';
16+
import * as pytest from '../client/unittests/pytest/main';
17+
import * as unittest from '../client/unittests/unittest/main';
18+
import { resolveValueAsTestToRun, getDiscoveredTests } from '../client/unittests/common/testUtils';
19+
import { BaseTestManager } from '../client/unittests/common/baseTestManager';
20+
import { PythonSettings, IUnitTestSettings } from '../client/common/configSettings';
21+
import { TestResultDisplay } from '../client/unittests/display/main';
22+
import { TestDisplay } from '../client/unittests/display/picker';
23+
import * as constants from '../client/common/constants';
24+
import { activateCodeLenses } from '../client/unittests/codeLenses/main';
25+
26+
let testManager: BaseTestManager;
27+
let pyTestManager: pytest.TestManager;
28+
let unittestManager: unittest.TestManager;
29+
let nosetestManager: nosetests.TestManager;
30+
let testResultDisplay: TestResultDisplay;
31+
let testDisplay: TestDisplay;
32+
let outChannel: vscode.OutputChannel;
33+
let lastRanTests: TestsToRun = null;
34+
35+
import * as path from 'path';
36+
import * as configSettings from '../client/common/configSettings';
37+
import * as fs from 'fs-extra';
38+
import { execPythonFile } from '../client/common/utils';
39+
40+
let pythonSettings = configSettings.PythonSettings.getInstance();
41+
const settings = PythonSettings.getInstance();
42+
43+
const UNITTEST_TEST_FILES_PATH = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'unitests');
44+
class MockOutputChannel implements vscode.OutputChannel {
45+
constructor(name: string) {
46+
this.name = name;
47+
this.output = '';
48+
}
49+
name: string;
50+
output: string;
51+
append(value: string) {
52+
this.output += value;
53+
}
54+
appendLine(value: string) { this.append(value); this.append('\n'); }
55+
clear() { }
56+
show(preservceFocus?: boolean): void;
57+
show(column?: vscode.ViewColumn, preserveFocus?: boolean): void;
58+
show(x?: any, y?: any): void { }
59+
hide() { }
60+
dispose() { }
61+
}
62+
63+
suiteSetup(done => {
64+
initialize().then(() => {
65+
done();
66+
});
67+
});
68+
suiteTeardown(done => {
69+
done();
70+
});
71+
72+
suite('Unit Tests (unittest)', () => {
73+
// setup(() => {
74+
// pythonSettings.unitTest.unittestEnabled = true;
75+
// pythonSettings.unitTest.nosetestsEnabled = false;
76+
// pythonSettings.unitTest.pyTestEnabled = false;
77+
// outChannel = new MockOutputChannel('Python Test Log');
78+
// });
79+
80+
test('Discover Tests', () => {
81+
const rootDirectory = UNITTEST_TEST_FILES_PATH;
82+
pythonSettings.unitTest.unittestArgs = [
83+
'-s=./tests',
84+
'-p=test_*.py'
85+
];
86+
const testManager = unittestManager ? unittestManager : new unittest.TestManager(rootDirectory, outChannel);
87+
if ((testManager.status !== TestStatus.Discovering && testManager.status !== TestStatus.Running)) {
88+
testResultDisplay = testResultDisplay ? testResultDisplay : new TestResultDisplay(outChannel);
89+
testManager.discoverTests(true, true).then(tests => {
90+
assert.equal(tests.testFiles.length, 2, 'Incorrect number of test files');
91+
assert.equal(tests.testFunctions.length, 6, 'Incorrect number of test functions');
92+
assert.equal(tests.testSuits.length, 3, 'Incorrect number of test suites');
93+
assert.equal(tests.testFiles.some(t=>t.name === 'test_unittest_one.py' && t.nameToRun === 'Test_test1.test_A'), true, 'Test File not found');
94+
assert.equal(tests.testFiles.some(t=>t.name === 'test_unittest_two.py' && t.nameToRun === 'Test_test2.test_A2'), true, 'Test File not found');
95+
}).catch(reason => {
96+
assert.fail(reason, null, 'Test Discovery failed', '');
97+
});
98+
}
99+
});
100+
101+
test('Discover Tests (pattern = *_test_*.py)', () => {
102+
const rootDirectory = UNITTEST_TEST_FILES_PATH;
103+
pythonSettings.unitTest.unittestArgs = [
104+
'-s=./tests',
105+
'-p=*_test*.py'
106+
];
107+
const testManager = unittestManager ? unittestManager : new unittest.TestManager(rootDirectory, outChannel);
108+
if ((testManager.status !== TestStatus.Discovering && testManager.status !== TestStatus.Running)) {
109+
testResultDisplay = testResultDisplay ? testResultDisplay : new TestResultDisplay(outChannel);
110+
testManager.discoverTests(true, true).then(tests => {
111+
assert.equal(tests.testFiles.length, 1, 'Incorrect number of test files');
112+
assert.equal(tests.testFunctions.length, 2, 'Incorrect number of test functions');
113+
assert.equal(tests.testSuits.length, 1, 'Incorrect number of test suites');
114+
assert.equal(tests.testFiles.some(t=>t.name === 'unittest_three_test.py' && t.nameToRun === 'Test_test3.test_A'), true, 'Test File not found');
115+
}).catch(reason => {
116+
assert.fail(reason, null, 'Test Discovery failed', '');
117+
});
118+
}
119+
});
120+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import sys
2+
import os
3+
4+
import unittest
5+
6+
class Test_test1(unittest.TestCase):
7+
def test_A(self):
8+
self.fail("Not implemented")
9+
10+
def test_B(self):
11+
self.assertEqual(1, 1, 'Not equal')
12+
13+
14+
if __name__ == '__main__':
15+
unittest.main()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import unittest
2+
3+
class Test_test2(unittest.TestCase):
4+
def test_A2(self):
5+
self.fail("Not implemented")
6+
7+
def test_B2(self):
8+
self.assertEqual(1,1,'Not equal')
9+
10+
class Test_test2a(unittest.TestCase):
11+
def test_222A2(self):
12+
self.fail("Not implemented")
13+
14+
def test_222B2(self):
15+
self.assertEqual(1,1,'Not equal')
16+
17+
class Test_test2a1(unittest.TestCase):
18+
def test_222A2wow(self):
19+
self.fail("Not implemented")
20+
21+
def test_222B2wow(self):
22+
self.assertEqual(1,1,'Not equal')
23+
24+
if __name__ == '__main__':
25+
unittest.main()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import unittest
2+
3+
4+
class Test_test3(unittest.TestCase):
5+
def test_A(self):
6+
self.fail("Not implemented")
7+
8+
def test_B(self):
9+
self.assertEqual(1, 1, 'Not equal')
10+
11+
12+
if __name__ == '__main__':
13+
unittest.main()

0 commit comments

Comments
 (0)