Skip to content

Commit ed06e55

Browse files
eleanorjboydbrettcannonkarthiknadig
authored
Edited Discovery Logic (#20631)
closes #20078 and closes #20085 (which is about the testing work to support this code) This logic now successfully works to discover the pytest repo tests. This branch takes into account all the previous comments made to the python discovery logic in the previous PR (located on my personal fork). Therefore this is a second round of edits on this code. It now works for the pytest library (discovers all the tests in the pytest library). --------- Co-authored-by: Brett Cannon <[email protected]> Co-authored-by: Karthik Nadig <[email protected]>
1 parent 7ac230c commit ed06e55

File tree

22 files changed

+1380
-55
lines changed

22 files changed

+1380
-55
lines changed

.vscode/launch.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,15 @@
253253
"request": "attach",
254254
"listen": { "host": "localhost", "port": 5678 },
255255
"justMyCode": true
256+
},
257+
{
258+
"name": "Debug pytest plugin tests",
259+
260+
"type": "python",
261+
"request": "launch",
262+
"module": "pytest",
263+
"args": ["${workspaceFolder}/pythonFiles/tests/pytestadapter"],
264+
"justMyCode": true
256265
}
257266
],
258267
"compounds": [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
5+
# This test's id is double_nested_folder/nested_folder_one/nested_folder_two/test_nest.py::test_function.
6+
# This test passes.
7+
def test_function(): # test_marker--test_function
8+
assert 1 == 1
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
5+
# This test's id is dual_level_nested_folder/nested_folder_one/test_bottom_folder.py::test_bottom_function_t.
6+
# This test passes.
7+
def test_bottom_function_t(): # test_marker--test_bottom_function_t
8+
assert True
9+
10+
11+
# This test's id is dual_level_nested_folder/nested_folder_one/test_bottom_folder.py::test_bottom_function_f.
12+
# This test fails.
13+
def test_bottom_function_f(): # test_marker--test_bottom_function_f
14+
assert False
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
5+
# This test's id is dual_level_nested_folder/test_top_folder.py::test_top_function_t.
6+
# This test passes.
7+
def test_top_function_t(): # test_marker--test_top_function_t
8+
assert True
9+
10+
11+
# This test's id is dual_level_nested_folder/test_top_folder.py::test_top_function_f.
12+
# This test fails.
13+
def test_top_function_f(): # test_marker--test_top_function_f
14+
assert False
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
5+
# This file has no tests in it; the discovery will return an empty list of tests.
6+
def function_function(string):
7+
return string
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
import pytest
4+
5+
6+
# This test has an error which will appear on pytest discovery.
7+
# This error is intentional and is meant to test pytest discovery error handling.
8+
@pytest.mark.parametrize("actual,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)])
9+
def test_function():
10+
assert True
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
# This test has a syntax error.
5+
# This error is intentional and is meant to test pytest discovery error handling.
6+
def test_function()
7+
assert True
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pytest
2+
3+
4+
# Testing pytest with parametrized tests. The first two pass, the third fails.
5+
# The tests ids are parametrize_tests.py::test_adding[3+5-8] and so on.
6+
@pytest.mark.parametrize( # test_marker--test_adding
7+
"actual, expected", [("3+5", 8), ("2+4", 6), ("6+9", 16)]
8+
)
9+
def test_adding(actual, expected):
10+
assert eval(actual) == expected
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
5+
# This test passes.
6+
def test_function(): # test_marker--test_function
7+
assert 1 == 1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This is a doctest test which passes #test_marker--text_docstring.txt
2+
>>> x = 3
3+
>>> x
4+
3
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
import unittest
4+
5+
6+
def add(a, b):
7+
return a + b
8+
9+
10+
class TestAddFunction(unittest.TestCase):
11+
# This test's id is unittest_folder/test_add.py::TestAddFunction::test_add_positive_numbers.
12+
# This test passes.
13+
def test_add_positive_numbers(self): # test_marker--test_add_positive_numbers
14+
result = add(2, 3)
15+
self.assertEqual(result, 5)
16+
17+
# This test's id is unittest_folder/test_add.py::TestAddFunction::test_add_negative_numbers.
18+
# This test passes.
19+
def test_add_negative_numbers(self): # test_marker--test_add_negative_numbers
20+
result = add(-2, -3)
21+
self.assertEqual(result, -5)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
import unittest
4+
5+
6+
def subtract(a, b):
7+
return a - b
8+
9+
10+
class TestSubtractFunction(unittest.TestCase):
11+
# This test's id is unittest_folder/test_subtract.py::TestSubtractFunction::test_subtract_positive_numbers.
12+
# This test passes.
13+
def test_subtract_positive_numbers( # test_marker--test_subtract_positive_numbers
14+
self,
15+
):
16+
result = subtract(5, 3)
17+
self.assertEqual(result, 2)
18+
19+
# This test's id is unittest_folder/test_subtract.py::TestSubtractFunction::test_subtract_negative_numbers.
20+
# This test passes.
21+
def test_subtract_negative_numbers( # test_marker--test_subtract_negative_numbers
22+
self,
23+
):
24+
result = subtract(-2, -3)
25+
self.assertEqual(result, 1)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import unittest
5+
6+
7+
class TestExample(unittest.TestCase):
8+
# This test's id is unittest_pytest_same_file.py::TestExample::test_true_unittest.
9+
# Test type is unittest and this test passes.
10+
def test_true_unittest(self): # test_marker--test_true_unittest
11+
assert True
12+
13+
14+
# This test's id is unittest_pytest_same_file.py::test_true_pytest.
15+
# Test type is pytest and this test passes.
16+
def test_true_pytest(): # test_marker--test_true_pytest
17+
assert True
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.

0 commit comments

Comments
 (0)