Skip to content

Commit ca14068

Browse files
committed
pytest tests
1 parent 29533cf commit ca14068

File tree

2 files changed

+240
-0
lines changed

2 files changed

+240
-0
lines changed

python_files/tests/pytestadapter/expected_discovery_test_output.py

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,3 +1870,201 @@
18701870
],
18711871
"id_": TEST_DATA_PATH_STR,
18721872
}
1873+
1874+
# =====================================================================================
1875+
# PROJECT_ROOT_PATH environment variable tests
1876+
# These test the project-based testing feature where PROJECT_ROOT_PATH changes
1877+
# the test tree root from cwd to the specified project path.
1878+
# =====================================================================================
1879+
1880+
# This is the expected output for unittest_folder when PROJECT_ROOT_PATH is set to unittest_folder.
1881+
# The root of the tree is unittest_folder (not .data), simulating project-based testing.
1882+
# └── unittest_folder (ROOT - set via PROJECT_ROOT_PATH)
1883+
# ├── test_add.py
1884+
# │ └── TestAddFunction
1885+
# │ ├── test_add_negative_numbers
1886+
# │ └── test_add_positive_numbers
1887+
# │ └── TestDuplicateFunction
1888+
# │ └── test_dup_a
1889+
# └── test_subtract.py
1890+
# └── TestSubtractFunction
1891+
# ├── test_subtract_negative_numbers
1892+
# └── test_subtract_positive_numbers
1893+
# └── TestDuplicateFunction
1894+
# └── test_dup_s
1895+
#
1896+
# Note: This reuses the unittest_folder paths defined earlier in this file.
1897+
project_root_unittest_folder_expected_output = {
1898+
"name": "unittest_folder",
1899+
"path": os.fspath(unittest_folder_path),
1900+
"type_": "folder",
1901+
"children": [
1902+
{
1903+
"name": "test_add.py",
1904+
"path": os.fspath(test_add_path),
1905+
"type_": "file",
1906+
"id_": os.fspath(test_add_path),
1907+
"children": [
1908+
{
1909+
"name": "TestAddFunction",
1910+
"path": os.fspath(test_add_path),
1911+
"type_": "class",
1912+
"children": [
1913+
{
1914+
"name": "test_add_negative_numbers",
1915+
"path": os.fspath(test_add_path),
1916+
"lineno": find_test_line_number(
1917+
"test_add_negative_numbers",
1918+
os.fspath(test_add_path),
1919+
),
1920+
"type_": "test",
1921+
"id_": get_absolute_test_id(
1922+
"test_add.py::TestAddFunction::test_add_negative_numbers",
1923+
test_add_path,
1924+
),
1925+
"runID": get_absolute_test_id(
1926+
"test_add.py::TestAddFunction::test_add_negative_numbers",
1927+
test_add_path,
1928+
),
1929+
},
1930+
{
1931+
"name": "test_add_positive_numbers",
1932+
"path": os.fspath(test_add_path),
1933+
"lineno": find_test_line_number(
1934+
"test_add_positive_numbers",
1935+
os.fspath(test_add_path),
1936+
),
1937+
"type_": "test",
1938+
"id_": get_absolute_test_id(
1939+
"test_add.py::TestAddFunction::test_add_positive_numbers",
1940+
test_add_path,
1941+
),
1942+
"runID": get_absolute_test_id(
1943+
"test_add.py::TestAddFunction::test_add_positive_numbers",
1944+
test_add_path,
1945+
),
1946+
},
1947+
],
1948+
"id_": get_absolute_test_id(
1949+
"test_add.py::TestAddFunction",
1950+
test_add_path,
1951+
),
1952+
"lineno": find_class_line_number("TestAddFunction", test_add_path),
1953+
},
1954+
{
1955+
"name": "TestDuplicateFunction",
1956+
"path": os.fspath(test_add_path),
1957+
"type_": "class",
1958+
"children": [
1959+
{
1960+
"name": "test_dup_a",
1961+
"path": os.fspath(test_add_path),
1962+
"lineno": find_test_line_number(
1963+
"test_dup_a",
1964+
os.fspath(test_add_path),
1965+
),
1966+
"type_": "test",
1967+
"id_": get_absolute_test_id(
1968+
"test_add.py::TestDuplicateFunction::test_dup_a",
1969+
test_add_path,
1970+
),
1971+
"runID": get_absolute_test_id(
1972+
"test_add.py::TestDuplicateFunction::test_dup_a",
1973+
test_add_path,
1974+
),
1975+
},
1976+
],
1977+
"id_": get_absolute_test_id(
1978+
"test_add.py::TestDuplicateFunction",
1979+
test_add_path,
1980+
),
1981+
"lineno": find_class_line_number("TestDuplicateFunction", test_add_path),
1982+
},
1983+
],
1984+
},
1985+
{
1986+
"name": "test_subtract.py",
1987+
"path": os.fspath(test_subtract_path),
1988+
"type_": "file",
1989+
"id_": os.fspath(test_subtract_path),
1990+
"children": [
1991+
{
1992+
"name": "TestSubtractFunction",
1993+
"path": os.fspath(test_subtract_path),
1994+
"type_": "class",
1995+
"children": [
1996+
{
1997+
"name": "test_subtract_negative_numbers",
1998+
"path": os.fspath(test_subtract_path),
1999+
"lineno": find_test_line_number(
2000+
"test_subtract_negative_numbers",
2001+
os.fspath(test_subtract_path),
2002+
),
2003+
"type_": "test",
2004+
"id_": get_absolute_test_id(
2005+
"test_subtract.py::TestSubtractFunction::test_subtract_negative_numbers",
2006+
test_subtract_path,
2007+
),
2008+
"runID": get_absolute_test_id(
2009+
"test_subtract.py::TestSubtractFunction::test_subtract_negative_numbers",
2010+
test_subtract_path,
2011+
),
2012+
},
2013+
{
2014+
"name": "test_subtract_positive_numbers",
2015+
"path": os.fspath(test_subtract_path),
2016+
"lineno": find_test_line_number(
2017+
"test_subtract_positive_numbers",
2018+
os.fspath(test_subtract_path),
2019+
),
2020+
"type_": "test",
2021+
"id_": get_absolute_test_id(
2022+
"test_subtract.py::TestSubtractFunction::test_subtract_positive_numbers",
2023+
test_subtract_path,
2024+
),
2025+
"runID": get_absolute_test_id(
2026+
"test_subtract.py::TestSubtractFunction::test_subtract_positive_numbers",
2027+
test_subtract_path,
2028+
),
2029+
},
2030+
],
2031+
"id_": get_absolute_test_id(
2032+
"test_subtract.py::TestSubtractFunction",
2033+
test_subtract_path,
2034+
),
2035+
"lineno": find_class_line_number("TestSubtractFunction", test_subtract_path),
2036+
},
2037+
{
2038+
"name": "TestDuplicateFunction",
2039+
"path": os.fspath(test_subtract_path),
2040+
"type_": "class",
2041+
"children": [
2042+
{
2043+
"name": "test_dup_s",
2044+
"path": os.fspath(test_subtract_path),
2045+
"lineno": find_test_line_number(
2046+
"test_dup_s",
2047+
os.fspath(test_subtract_path),
2048+
),
2049+
"type_": "test",
2050+
"id_": get_absolute_test_id(
2051+
"test_subtract.py::TestDuplicateFunction::test_dup_s",
2052+
test_subtract_path,
2053+
),
2054+
"runID": get_absolute_test_id(
2055+
"test_subtract.py::TestDuplicateFunction::test_dup_s",
2056+
test_subtract_path,
2057+
),
2058+
},
2059+
],
2060+
"id_": get_absolute_test_id(
2061+
"test_subtract.py::TestDuplicateFunction",
2062+
test_subtract_path,
2063+
),
2064+
"lineno": find_class_line_number("TestDuplicateFunction", test_subtract_path),
2065+
},
2066+
],
2067+
},
2068+
],
2069+
"id_": os.fspath(unittest_folder_path),
2070+
}

python_files/tests/pytestadapter/test_discovery.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,45 @@ def test_plugin_collect(file, expected_const, extra_arg):
386386
), (
387387
f"Tests tree does not match expected value. \n Expected: {json.dumps(expected_const, indent=4)}. \n Actual: {json.dumps(actual_item.get('tests'), indent=4)}"
388388
)
389+
390+
391+
def test_project_root_path_env_var():
392+
"""Test pytest discovery with PROJECT_ROOT_PATH environment variable set.
393+
394+
This simulates project-based testing where the test tree root should be
395+
the project root (PROJECT_ROOT_PATH) rather than the workspace cwd.
396+
397+
When PROJECT_ROOT_PATH is set:
398+
- The test tree root (name, path, id_) should match PROJECT_ROOT_PATH
399+
- The cwd in the response should match PROJECT_ROOT_PATH
400+
- Test files should be direct children of the root (not nested under a subfolder)
401+
"""
402+
# Use unittest_folder as our "project" subdirectory
403+
project_path = helpers.TEST_DATA_PATH / "unittest_folder"
404+
405+
actual = helpers.runner_with_cwd_env(
406+
[os.fspath(project_path), "--collect-only"],
407+
helpers.TEST_DATA_PATH, # cwd is parent of project
408+
{"PROJECT_ROOT_PATH": os.fspath(project_path)}, # Set project root
409+
)
410+
411+
assert actual
412+
actual_list: List[Dict[str, Any]] = actual
413+
if actual_list is not None:
414+
actual_item = actual_list.pop(0)
415+
416+
assert all(item in actual_item for item in ("status", "cwd", "error"))
417+
assert actual_item.get("status") == "success", (
418+
f"Status is not 'success', error is: {actual_item.get('error')}"
419+
)
420+
# cwd in response should be PROJECT_ROOT_PATH
421+
assert actual_item.get("cwd") == os.fspath(project_path), (
422+
f"Expected cwd '{os.fspath(project_path)}', got '{actual_item.get('cwd')}'"
423+
)
424+
assert is_same_tree(
425+
actual_item.get("tests"),
426+
expected_discovery_test_output.project_root_unittest_folder_expected_output,
427+
["id_", "lineno", "name", "runID"],
428+
), (
429+
f"Tests tree does not match expected value. \n Expected: {json.dumps(expected_discovery_test_output.project_root_unittest_folder_expected_output, indent=4)}. \n Actual: {json.dumps(actual_item.get('tests'), indent=4)}"
430+
)

0 commit comments

Comments
 (0)