Skip to content

Commit

Permalink
Edited Discovery Logic (#20631)
Browse files Browse the repository at this point in the history
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 <brcan@microsoft.com>
Co-authored-by: Karthik Nadig <kanadig@microsoft.com>
  • Loading branch information
3 people authored Apr 17, 2023
1 parent 7ac230c commit ed06e55
Show file tree
Hide file tree
Showing 22 changed files with 1,380 additions and 55 deletions.
9 changes: 9 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@
"request": "attach",
"listen": { "host": "localhost", "port": 5678 },
"justMyCode": true
},
{
"name": "Debug pytest plugin tests",

"type": "python",
"request": "launch",
"module": "pytest",
"args": ["${workspaceFolder}/pythonFiles/tests/pytestadapter"],
"justMyCode": true
}
],
"compounds": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.


# This test's id is double_nested_folder/nested_folder_one/nested_folder_two/test_nest.py::test_function.
# This test passes.
def test_function(): # test_marker--test_function
assert 1 == 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.


# This test's id is dual_level_nested_folder/nested_folder_one/test_bottom_folder.py::test_bottom_function_t.
# This test passes.
def test_bottom_function_t(): # test_marker--test_bottom_function_t
assert True


# This test's id is dual_level_nested_folder/nested_folder_one/test_bottom_folder.py::test_bottom_function_f.
# This test fails.
def test_bottom_function_f(): # test_marker--test_bottom_function_f
assert False
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.


# This test's id is dual_level_nested_folder/test_top_folder.py::test_top_function_t.
# This test passes.
def test_top_function_t(): # test_marker--test_top_function_t
assert True


# This test's id is dual_level_nested_folder/test_top_folder.py::test_top_function_f.
# This test fails.
def test_top_function_f(): # test_marker--test_top_function_f
assert False
7 changes: 7 additions & 0 deletions pythonFiles/tests/pytestadapter/.data/empty_discovery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.


# This file has no tests in it; the discovery will return an empty list of tests.
def function_function(string):
return string
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import pytest


# This test has an error which will appear on pytest discovery.
# This error is intentional and is meant to test pytest discovery error handling.
@pytest.mark.parametrize("actual,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)])
def test_function():
assert True
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

# This test has a syntax error.
# This error is intentional and is meant to test pytest discovery error handling.
def test_function()
assert True
10 changes: 10 additions & 0 deletions pythonFiles/tests/pytestadapter/.data/parametrize_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest


# Testing pytest with parametrized tests. The first two pass, the third fails.
# The tests ids are parametrize_tests.py::test_adding[3+5-8] and so on.
@pytest.mark.parametrize( # test_marker--test_adding
"actual, expected", [("3+5", 8), ("2+4", 6), ("6+9", 16)]
)
def test_adding(actual, expected):
assert eval(actual) == expected
7 changes: 7 additions & 0 deletions pythonFiles/tests/pytestadapter/.data/simple_pytest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.


# This test passes.
def test_function(): # test_marker--test_function
assert 1 == 1
4 changes: 4 additions & 0 deletions pythonFiles/tests/pytestadapter/.data/text_docstring.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This is a doctest test which passes #test_marker--text_docstring.txt
>>> x = 3
>>> x
3
21 changes: 21 additions & 0 deletions pythonFiles/tests/pytestadapter/.data/unittest_folder/test_add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import unittest


def add(a, b):
return a + b


class TestAddFunction(unittest.TestCase):
# This test's id is unittest_folder/test_add.py::TestAddFunction::test_add_positive_numbers.
# This test passes.
def test_add_positive_numbers(self): # test_marker--test_add_positive_numbers
result = add(2, 3)
self.assertEqual(result, 5)

# This test's id is unittest_folder/test_add.py::TestAddFunction::test_add_negative_numbers.
# This test passes.
def test_add_negative_numbers(self): # test_marker--test_add_negative_numbers
result = add(-2, -3)
self.assertEqual(result, -5)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import unittest


def subtract(a, b):
return a - b


class TestSubtractFunction(unittest.TestCase):
# This test's id is unittest_folder/test_subtract.py::TestSubtractFunction::test_subtract_positive_numbers.
# This test passes.
def test_subtract_positive_numbers( # test_marker--test_subtract_positive_numbers
self,
):
result = subtract(5, 3)
self.assertEqual(result, 2)

# This test's id is unittest_folder/test_subtract.py::TestSubtractFunction::test_subtract_negative_numbers.
# This test passes.
def test_subtract_negative_numbers( # test_marker--test_subtract_negative_numbers
self,
):
result = subtract(-2, -3)
self.assertEqual(result, 1)
17 changes: 17 additions & 0 deletions pythonFiles/tests/pytestadapter/.data/unittest_pytest_same_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import unittest


class TestExample(unittest.TestCase):
# This test's id is unittest_pytest_same_file.py::TestExample::test_true_unittest.
# Test type is unittest and this test passes.
def test_true_unittest(self): # test_marker--test_true_unittest
assert True


# This test's id is unittest_pytest_same_file.py::test_true_pytest.
# Test type is pytest and this test passes.
def test_true_pytest(): # test_marker--test_true_pytest
assert True
2 changes: 2 additions & 0 deletions pythonFiles/tests/pytestadapter/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Loading

0 comments on commit ed06e55

Please sign in to comment.