|
| 1 | +import json |
| 2 | + |
| 3 | + |
| 4 | +def pytest_addoption(parser): |
| 5 | + parser.addoption( |
| 6 | + "--endpoint", action="store", default="all", help="my option: endpoints" |
| 7 | + ) |
| 8 | + |
| 9 | + |
| 10 | +def pytest_configure(config): |
| 11 | + config.addinivalue_line( |
| 12 | + "markers", "endpoint(endpoint): this mark select the test based on endpoint" |
| 13 | + ) |
| 14 | + |
| 15 | + |
| 16 | +def pytest_runtest_setup(item): |
| 17 | + getoption = item.config.getoption("--endpoint").split(",") |
| 18 | + if getoption not in (["all"], [''], [""]): |
| 19 | + endpoint_names = [mark.args[0] for mark in item.iter_markers(name="endpoint")] |
| 20 | + if not endpoint_names or not set(getoption).intersection(set(endpoint_names)): |
| 21 | + pytest.skip("Test skipped because endpoint is {!r}".format(endpoint_names)) |
| 22 | + |
| 23 | + |
1 | 24 | def pytest_collection_modifyitems(items): |
| 25 | + # load the JSON file |
| 26 | + with open("tests/endpoints_mapping.json", "r") as json_file: |
| 27 | + endpoints_file_mapping = json.load(json_file) |
| 28 | + |
| 29 | + # create a dictionary to map filenames to endpoints |
| 30 | + filename_to_endpoint = {} |
| 31 | + for endpoint, files in endpoints_file_mapping.items(): |
| 32 | + for filename in files: |
| 33 | + filename_to_endpoint[filename] = endpoint |
| 34 | + |
| 35 | + # add the markers based on the JSON file |
2 | 36 | for item in items: |
3 | | - # add the name of the file (without extension) as a marker |
4 | | - filename = item.nodeid.split("::")[0].split("/")[-1].replace(".py", "") |
5 | | - marker = pytest.mark.file(filename) |
6 | | - item.add_marker(marker) |
| 37 | + # map the name of the file to endpoint, else use default value |
| 38 | + filename = item.fspath.basename |
| 39 | + marker = filename_to_endpoint.get(filename, filename) |
| 40 | + item.add_marker(pytest.mark.endpoint(marker, filename=filename)) |
0 commit comments