Skip to content

Commit

Permalink
add support for pytest describe (#24400)
Browse files Browse the repository at this point in the history
fixes #21705
  • Loading branch information
eleanorjboyd authored Nov 8, 2024
1 parent b1cb5c2 commit 785ed68
Show file tree
Hide file tree
Showing 8 changed files with 385 additions and 99 deletions.
3 changes: 3 additions & 0 deletions build/test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ django-stubs
# for coverage
coverage
pytest-cov

# for pytest-describe related tests
pytest-describe
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

def describe_A():
def test_1(): # test_marker--test_1
pass

def test_2(): # test_marker--test_2
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import pytest


def describe_list():
@pytest.fixture
def list():
return []

def describe_append():
def add_empty(list): # test_marker--add_empty
list.append("foo")
list.append("bar")
assert list == ["foo", "bar"]

def remove_empty(list): # test_marker--remove_empty
try:
list.remove("foo")
except ValueError:
pass

def describe_remove():
@pytest.fixture
def list():
return ["foo", "bar"]

def removes(list): # test_marker--removes
list.remove("foo")
assert list == ["bar"]
183 changes: 183 additions & 0 deletions python_files/tests/pytestadapter/expected_discovery_test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -1394,3 +1394,186 @@
],
"id_": TEST_DATA_PATH_STR,
}
# This is the expected output for the describe_only.py tests.
# └── describe_only.py
# └── describe_A
# └── test_1
# └── test_2

describe_only_path = TEST_DATA_PATH / "pytest_describe_plugin" / "describe_only.py"
pytest_describe_plugin_path = TEST_DATA_PATH / "pytest_describe_plugin"

expected_describe_only_output = {
"name": ".data",
"path": TEST_DATA_PATH_STR,
"type_": "folder",
"children": [
{
"name": "pytest_describe_plugin",
"path": os.fspath(pytest_describe_plugin_path),
"type_": "folder",
"id_": os.fspath(pytest_describe_plugin_path),
"children": [
{
"name": "describe_only.py",
"path": os.fspath(describe_only_path),
"type_": "file",
"id_": os.fspath(describe_only_path),
"children": [
{
"name": "describe_A",
"path": os.fspath(describe_only_path),
"type_": "class",
"children": [
{
"name": "test_1",
"path": os.fspath(describe_only_path),
"lineno": find_test_line_number(
"test_1",
describe_only_path,
),
"type_": "test",
"id_": get_absolute_test_id(
"pytest_describe_plugin/describe_only.py::describe_A::test_1",
describe_only_path,
),
"runID": get_absolute_test_id(
"pytest_describe_plugin/describe_only.py::describe_A::test_1",
describe_only_path,
),
},
{
"name": "test_2",
"path": os.fspath(describe_only_path),
"lineno": find_test_line_number(
"test_2",
describe_only_path,
),
"type_": "test",
"id_": get_absolute_test_id(
"pytest_describe_plugin/describe_only.py::describe_A::test_2",
describe_only_path,
),
"runID": get_absolute_test_id(
"pytest_describe_plugin/describe_only.py::describe_A::test_2",
describe_only_path,
),
},
],
"id_": "pytest_describe_plugin/describe_only.py::describe_A",
}
],
}
],
}
],
"id_": TEST_DATA_PATH_STR,
}
# This is the expected output for the nested_describe.py tests.
# └── nested_describe.py
# └── describe_list
# └── describe_append
# └── add_empty
# └── remove_empty
# └── describe_remove
# └── removes
nested_describe_path = TEST_DATA_PATH / "pytest_describe_plugin" / "nested_describe.py"
expected_nested_describe_output = {
"name": ".data",
"path": TEST_DATA_PATH_STR,
"type_": "folder",
"children": [
{
"name": "pytest_describe_plugin",
"path": os.fspath(pytest_describe_plugin_path),
"type_": "folder",
"id_": os.fspath(pytest_describe_plugin_path),
"children": [
{
"name": "nested_describe.py",
"path": os.fspath(nested_describe_path),
"type_": "file",
"id_": os.fspath(nested_describe_path),
"children": [
{
"name": "describe_list",
"path": os.fspath(nested_describe_path),
"type_": "class",
"children": [
{
"name": "describe_append",
"path": os.fspath(nested_describe_path),
"type_": "class",
"children": [
{
"name": "add_empty",
"path": os.fspath(nested_describe_path),
"lineno": find_test_line_number(
"add_empty",
nested_describe_path,
),
"type_": "test",
"id_": get_absolute_test_id(
"pytest_describe_plugin/nested_describe.py::describe_list::describe_append::add_empty",
nested_describe_path,
),
"runID": get_absolute_test_id(
"pytest_describe_plugin/nested_describe.py::describe_list::describe_append::add_empty",
nested_describe_path,
),
},
{
"name": "remove_empty",
"path": os.fspath(nested_describe_path),
"lineno": find_test_line_number(
"remove_empty",
nested_describe_path,
),
"type_": "test",
"id_": get_absolute_test_id(
"pytest_describe_plugin/nested_describe.py::describe_list::describe_append::remove_empty",
nested_describe_path,
),
"runID": get_absolute_test_id(
"pytest_describe_plugin/nested_describe.py::describe_list::describe_append::remove_empty",
nested_describe_path,
),
},
],
"id_": "pytest_describe_plugin/nested_describe.py::describe_list::describe_append",
},
{
"name": "describe_remove",
"path": os.fspath(nested_describe_path),
"type_": "class",
"children": [
{
"name": "removes",
"path": os.fspath(nested_describe_path),
"lineno": find_test_line_number(
"removes",
nested_describe_path,
),
"type_": "test",
"id_": get_absolute_test_id(
"pytest_describe_plugin/nested_describe.py::describe_list::describe_remove::removes",
nested_describe_path,
),
"runID": get_absolute_test_id(
"pytest_describe_plugin/nested_describe.py::describe_list::describe_remove::removes",
nested_describe_path,
),
}
],
"id_": "pytest_describe_plugin/nested_describe.py::describe_list::describe_remove",
},
],
"id_": "pytest_describe_plugin/nested_describe.py::describe_list",
}
],
}
],
}
],
"id_": TEST_DATA_PATH_STR,
}
88 changes: 88 additions & 0 deletions python_files/tests/pytestadapter/expected_execution_test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,91 @@
"subtest": None,
}
}


# This is the expected output for the pytest_describe_plugin/describe_only.py file.
# └── pytest_describe_plugin
# └── describe_only.py
# └── describe_A
# └── test_1: success
# └── test_2: success

describe_only_expected_execution_output = {
get_absolute_test_id(
"pytest_describe_plugin/describe_only.py::describe_A::test_1",
TEST_DATA_PATH / "pytest_describe_plugin" / "describe_only.py",
): {
"test": get_absolute_test_id(
"pytest_describe_plugin/describe_only.py::describe_A::test_1",
TEST_DATA_PATH / "pytest_describe_plugin" / "describe_only.py",
),
"outcome": "success",
"message": None,
"traceback": None,
"subtest": None,
},
get_absolute_test_id(
"pytest_describe_plugin/describe_only.py::describe_A::test_2",
TEST_DATA_PATH / "pytest_describe_plugin" / "describe_only.py",
): {
"test": get_absolute_test_id(
"pytest_describe_plugin/describe_only.py::describe_A::test_2",
TEST_DATA_PATH / "pytest_describe_plugin" / "describe_only.py",
),
"outcome": "success",
"message": None,
"traceback": None,
"subtest": None,
},
}

# This is the expected output for the pytest_describe_plugin/nested_describe.py file.
# └── pytest_describe_plugin
# └── nested_describe.py
# └── describe_list
# └── describe_append
# └── add_empty: success
# └── remove_empty: success
# └── describe_remove
# └── removes: success
nested_describe_expected_execution_output = {
get_absolute_test_id(
"pytest_describe_plugin/nested_describe.py::describe_list::describe_append::add_empty",
TEST_DATA_PATH / "pytest_describe_plugin" / "nested_describe.py",
): {
"test": get_absolute_test_id(
"pytest_describe_plugin/nested_describe.py::describe_list::describe_append::add_empty",
TEST_DATA_PATH / "pytest_describe_plugin" / "nested_describe.py",
),
"outcome": "success",
"message": None,
"traceback": None,
"subtest": None,
},
get_absolute_test_id(
"pytest_describe_plugin/nested_describe.py::describe_list::describe_append::remove_empty",
TEST_DATA_PATH / "pytest_describe_plugin" / "nested_describe.py",
): {
"test": get_absolute_test_id(
"pytest_describe_plugin/nested_describe.py::describe_list::describe_append::remove_empty",
TEST_DATA_PATH / "pytest_describe_plugin" / "nested_describe.py",
),
"outcome": "success",
"message": None,
"traceback": None,
"subtest": None,
},
get_absolute_test_id(
"pytest_describe_plugin/nested_describe.py::describe_list::describe_remove::removes",
TEST_DATA_PATH / "pytest_describe_plugin" / "nested_describe.py",
): {
"test": get_absolute_test_id(
"pytest_describe_plugin/nested_describe.py::describe_list::describe_remove::removes",
TEST_DATA_PATH / "pytest_describe_plugin" / "nested_describe.py",
),
"outcome": "success",
"message": None,
"traceback": None,
"subtest": None,
},
}
8 changes: 8 additions & 0 deletions python_files/tests/pytestadapter/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ def test_parameterized_error_collect():
"text_docstring.txt",
expected_discovery_test_output.doctest_pytest_expected_output,
),
(
"pytest_describe_plugin" + os.path.sep + "describe_only.py",
expected_discovery_test_output.expected_describe_only_output,
),
(
"pytest_describe_plugin" + os.path.sep + "nested_describe.py",
expected_discovery_test_output.expected_nested_describe_output,
),
],
)
def test_pytest_collect(file, expected_const):
Expand Down
Loading

0 comments on commit 785ed68

Please sign in to comment.