Skip to content

Commit b866a5f

Browse files
committed
test pytest with config
1 parent df0bf9c commit b866a5f

9 files changed

Lines changed: 217 additions & 4 deletions

File tree

src/test/extension.unittests.pytest.test.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as assert from 'assert';
1212
// You can import and use all API from the \'vscode\' module
1313
// as well as import your extension to test it
1414
import * as vscode from 'vscode';
15-
import { TestsToRun } from '../client/unittests/common/contracts';
15+
import { TestsToRun, TestFile, TestFunction, TestSuite } from '../client/unittests/common/contracts';
1616
import * as pytest from '../client/unittests/pytest/main';
1717
import { TestResultDisplay } from '../client/unittests/display/main';
1818

@@ -23,6 +23,8 @@ import * as configSettings from '../client/common/configSettings';
2323
let pythonSettings = configSettings.PythonSettings.getInstance();
2424

2525
const UNITTEST_TEST_FILES_PATH = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'unitests');
26+
const UNITTEST_TEST_FILES_PATH_WITH_CONFIGS = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'unitestsWithConfigs');
27+
2628
class MockOutputChannel implements vscode.OutputChannel {
2729
constructor(name: string) {
2830
this.name = name;
@@ -53,6 +55,7 @@ suite('Unit Tests (PyTest)', () => {
5355
done();
5456
});
5557
setup(() => {
58+
rootDirectory = UNITTEST_TEST_FILES_PATH;
5659
outChannel = new MockOutputChannel('Python Test Log');
5760
testResultDisplay = new TestResultDisplay(outChannel);
5861
});
@@ -64,7 +67,7 @@ suite('Unit Tests (PyTest)', () => {
6467
function createTestManager() {
6568
testManager = new pytest.TestManager(rootDirectory, outChannel);
6669
}
67-
const rootDirectory = UNITTEST_TEST_FILES_PATH;
70+
let rootDirectory = UNITTEST_TEST_FILES_PATH;
6871
let testManager: pytest.TestManager;
6972
let testResultDisplay: TestResultDisplay;
7073
let outChannel: vscode.OutputChannel;
@@ -101,6 +104,21 @@ suite('Unit Tests (PyTest)', () => {
101104
}).catch(done);
102105
});
103106

107+
108+
test('Discover Tests (with config)', done => {
109+
pythonSettings.unitTest.pyTestArgs = [];
110+
rootDirectory = UNITTEST_TEST_FILES_PATH_WITH_CONFIGS;
111+
createTestManager();
112+
testManager.discoverTests(true, true).then(tests => {
113+
assert.equal(tests.testFiles.length, 2, 'Incorrect number of test files');
114+
assert.equal(tests.testFunctions.length, 14, 'Incorrect number of test functions');
115+
assert.equal(tests.testSuits.length, 4, 'Incorrect number of test suites');
116+
assert.equal(tests.testFiles.some(t => t.name === 'other/test_unittest_one.py' && t.nameToRun === t.name), true, 'Test File not found');
117+
assert.equal(tests.testFiles.some(t => t.name === 'other/test_pytest.py' && t.nameToRun === t.name), true, 'Test File not found');
118+
done();
119+
}).catch(done);
120+
});
121+
104122
test('Run Tests', done => {
105123
pythonSettings.unitTest.pyTestArgs = [
106124
'-k=test_'
@@ -142,8 +160,17 @@ suite('Unit Tests (PyTest)', () => {
142160
];
143161
createTestManager();
144162
testManager.discoverTests(true, true).then(tests => {
145-
const testFile: TestsToRun = { testFile: [tests.testFiles[0]], testFolder: [], testFunction: [], testSuite: [] };
146-
return testManager.runTest(testFile).then(tests => {
163+
const testFile: TestFile = {
164+
fullPath: path.join(rootDirectory, 'tests', 'test_another_pytest.py'),
165+
name: 'tests/test_another_pytest.py',
166+
nameToRun: 'tests/test_another_pytest.py',
167+
xmlName: 'tests/test_another_pytest.py',
168+
functions: [],
169+
suites: [],
170+
time: 0
171+
};
172+
const testFileToRun: TestsToRun = { testFile: [testFile], testFolder: [], testFunction: [], testSuite: [] };
173+
return testManager.runTest(testFileToRun).then(tests => {
147174
assert.equal(tests.summary.errors, 0, 'Errors');
148175
assert.equal(tests.summary.failures, 1, 'Failures');
149176
assert.equal(tests.summary.passed, 3, 'Passed');
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# content of tests/test_something.py
2+
import pytest
3+
import unittest
4+
5+
# content of check_myapp.py
6+
class Test_CheckMyApp:
7+
@unittest.skip("demonstrating skipping")
8+
def test_simple_check(self):
9+
pass
10+
def test_complex_check(self):
11+
pass
12+
13+
class Test_NestedClassA:
14+
def test_nested_class_methodB(self):
15+
assert True
16+
class Test_nested_classB_Of_A:
17+
def test_d(self):
18+
assert True
19+
def test_nested_class_methodC(self):
20+
assert True
21+
22+
def test_simple_check2(self):
23+
pass
24+
def test_complex_check2(self):
25+
pass
26+
27+
28+
@pytest.fixture
29+
def parametrized_username():
30+
return 'overridden-username'
31+
32+
@pytest.fixture(params=['one', 'two', 'three'])
33+
def non_parametrized_username(request):
34+
return request.param
35+
36+
def test_username(parametrized_username):
37+
assert parametrized_username == 'overridden-username'
38+
39+
def test_parametrized_username(non_parametrized_username):
40+
assert non_parametrized_username in ['one', 'two', 'threes']
41+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
@unittest.skip("demonstrating skipping")
14+
def test_c(self):
15+
self.assertEqual(1, 1, 'Not equal')
16+
17+
18+
if __name__ == '__main__':
19+
unittest.main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# content of pytest.ini
2+
[pytest]
3+
testpaths = other
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# content of tests/test_something.py
2+
import pytest
3+
import unittest
4+
5+
@pytest.fixture
6+
def parametrized_username():
7+
return 'overridden-username'
8+
9+
@pytest.fixture(params=['one', 'two', 'three'])
10+
def non_parametrized_username(request):
11+
return request.param
12+
13+
def test_username(parametrized_username):
14+
assert parametrized_username == 'overridden-username'
15+
16+
def test_parametrized_username(non_parametrized_username):
17+
assert non_parametrized_username in ['one', 'two', 'threes']
18+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# content of tests/test_something.py
2+
import pytest
3+
import unittest
4+
5+
# content of check_myapp.py
6+
class Test_CheckMyApp:
7+
@unittest.skip("demonstrating skipping")
8+
def test_simple_check(self):
9+
pass
10+
def test_complex_check(self):
11+
pass
12+
13+
class Test_NestedClassA:
14+
def test_nested_class_methodB(self):
15+
assert True
16+
class Test_nested_classB_Of_A:
17+
def test_d(self):
18+
assert True
19+
def test_nested_class_methodC(self):
20+
assert True
21+
22+
def test_simple_check2(self):
23+
pass
24+
def test_complex_check2(self):
25+
pass
26+
27+
28+
@pytest.fixture
29+
def parametrized_username():
30+
return 'overridden-username'
31+
32+
@pytest.fixture(params=['one', 'two', 'three'])
33+
def non_parametrized_username(request):
34+
return request.param
35+
36+
def test_username(parametrized_username):
37+
assert parametrized_username == 'overridden-username'
38+
39+
def test_parametrized_username(non_parametrized_username):
40+
assert non_parametrized_username in ['one', 'two', 'threes']
41+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
@unittest.skip("demonstrating skipping")
14+
def test_c(self):
15+
self.assertEqual(1, 1, 'Not equal')
16+
17+
18+
if __name__ == '__main__':
19+
unittest.main()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
def test_C2(self):
11+
self.assertEqual(1,2,'Not equal')
12+
13+
def test_D2(self):
14+
raise ArithmeticError()
15+
pass
16+
17+
class Test_test2a(unittest.TestCase):
18+
def test_222A2(self):
19+
self.fail("Not implemented")
20+
21+
def test_222B2(self):
22+
self.assertEqual(1,1,'Not equal')
23+
24+
class Test_test2a1(unittest.TestCase):
25+
def test_222A2wow(self):
26+
self.fail("Not implemented")
27+
28+
def test_222B2wow(self):
29+
self.assertEqual(1,1,'Not equal')
30+
31+
if __name__ == '__main__':
32+
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)