|
| 1 | +--- |
| 2 | +applyTo: 'python_files/tests/pytestadapter/test_discovery.py' |
| 3 | +description: 'A guide for adding new tests for pytest discovery and JSON formatting in the test_pytest_collect suite.' |
| 4 | +--- |
| 5 | + |
| 6 | +# How to Add New Pytest Discovery Tests |
| 7 | + |
| 8 | +This guide explains how to add new tests for pytest discovery and JSON formatting in the `test_pytest_collect` suite. Follow these steps to ensure your tests are consistent and correct. |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## 1. Add Your Test File |
| 13 | + |
| 14 | +- Place your new test file/files in the appropriate subfolder under: |
| 15 | + ``` |
| 16 | + python_files/tests/pytestadapter/.data/ |
| 17 | + ``` |
| 18 | +- Organize folders and files to match the structure you want to test. For example, to test nested folders, create the corresponding directory structure. |
| 19 | +- In your test file, mark each test function with a comment: |
| 20 | + ```python |
| 21 | + def test_function(): # test_marker--test_function |
| 22 | + ... |
| 23 | + ``` |
| 24 | +
|
| 25 | +**Root Node Matching:** |
| 26 | +
|
| 27 | +- The root node in your expected output must match the folder or file you pass to pytest discovery. For example, if you run discovery on a subfolder, the root `"name"`, `"path"`, and `"id_"` in your expected output should be that subfolder, not the parent `.data` folder. |
| 28 | +- Only use `.data` as the root if you are running discovery on the entire `.data` folder. |
| 29 | +
|
| 30 | +**Example:** |
| 31 | +If you run: |
| 32 | +
|
| 33 | +```python |
| 34 | +helpers.runner([os.fspath(TEST_DATA_PATH / "myfolder"), "--collect-only"]) |
| 35 | +``` |
| 36 | + |
| 37 | +then your expected output root should be: |
| 38 | + |
| 39 | +```python |
| 40 | +{ |
| 41 | + "name": "myfolder", |
| 42 | + "path": os.fspath(TEST_DATA_PATH / "myfolder"), |
| 43 | + "type_": "folder", |
| 44 | + ... |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +## 2. Update `expected_discovery_test_output.py` |
| 51 | + |
| 52 | +- Open `expected_discovery_test_output.py` in the same test suite. |
| 53 | +- Add a new expected output dictionary for your test file, following the format of existing entries. |
| 54 | +- Use the helper functions and path conventions: |
| 55 | + - Use `os.fspath()` for all paths. |
| 56 | + - Use `find_test_line_number("function_name", file_path)` for the `lineno` field. |
| 57 | + - Use `get_absolute_test_id("relative_path::function_name", file_path)` for `id_` and `runID`. |
| 58 | + - Always use current path concatenation (e.g., `TEST_DATA_PATH / "your_folder" / "your_file.py"`). |
| 59 | + - Create new constants as needed to keep the code clean and maintainable. |
| 60 | + |
| 61 | +**Important:** |
| 62 | + |
| 63 | +- Do **not** read the entire `expected_discovery_test_output.py` file if you only need to add or reference a single constant. This file is very large; prefer searching for the relevant section or appending to the end. |
| 64 | + |
| 65 | +**Example:** |
| 66 | +If you run discovery on a subfolder: |
| 67 | + |
| 68 | +```python |
| 69 | +helpers.runner([os.fspath(TEST_DATA_PATH / "myfolder"), "--collect-only"]) |
| 70 | +``` |
| 71 | + |
| 72 | +then your expected output root should be: |
| 73 | + |
| 74 | +```python |
| 75 | +myfolder_path = TEST_DATA_PATH / "myfolder" |
| 76 | +my_expected_output = { |
| 77 | + "name": "myfolder", |
| 78 | + "path": os.fspath(myfolder_path), |
| 79 | + "type_": "folder", |
| 80 | + ... |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +- Add a comment above your dictionary describing the structure, as in the existing examples. |
| 85 | + |
| 86 | +--- |
| 87 | + |
| 88 | +## 3. Add Your Test to `test_discovery.py` |
| 89 | + |
| 90 | +- In `test_discovery.py`, add your new test as a parameterized case to the main `test_pytest_collect` function. Do **not** create a standalone test function for new discovery cases. |
| 91 | +- Reference your new expected output constant from `expected_discovery_test_output.py`. |
| 92 | + |
| 93 | +**Example:** |
| 94 | + |
| 95 | +```python |
| 96 | +@pytest.mark.parametrize( |
| 97 | + ("file", "expected_const"), |
| 98 | + [ |
| 99 | + ("myfolder", my_expected_output), |
| 100 | + # ... other cases ... |
| 101 | + ], |
| 102 | +) |
| 103 | +def test_pytest_collect(file, expected_const): |
| 104 | + ... |
| 105 | +``` |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +## 4. Run and Verify |
| 110 | + |
| 111 | +- Run the test suite to ensure your new test is discovered and passes. |
| 112 | +- If the test fails, check your expected output dictionary for path or structure mismatches. |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +## 5. Tips |
| 117 | + |
| 118 | +- Always use the helper functions for line numbers and IDs. |
| 119 | +- Match the folder/file structure in `.data` to the expected JSON structure. |
| 120 | +- Use comments to document the expected output structure for clarity. |
| 121 | +- Ensure all `"path"` and `"id_"` fields in your expected output match exactly what pytest returns, including absolute paths and root node structure. |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +**Reference:** |
| 126 | +See `expected_discovery_test_output.py` for more examples and formatting. Use search or jump to the end of the file to avoid reading the entire file when possible. |
0 commit comments