Skip to content

Commit

Permalink
Set fixture remove_document_file on autouse (#132)
Browse files Browse the repository at this point in the history
* Run fixture remove_document_file automatically on all tests to remove file linked in testcases

* Replaced try/except in fixture with if statement
  • Loading branch information
tilen1976 authored Jan 24, 2024
1 parent 770c7da commit d2fa8c8
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 12 deletions.
7 changes: 2 additions & 5 deletions tests/backend/test_datadoc_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_get_dataset_state(
assert actual_state == expected_result


@pytest.mark.usefixtures("existing_metadata_file", "remove_document_file")
@pytest.mark.usefixtures("existing_metadata_file")
def test_existing_metadata_file(
metadata: DataDocMetadata,
):
Expand Down Expand Up @@ -99,7 +99,6 @@ def test_get_dataset_version(
assert DataDocMetadata.get_dataset_version(short_name) == expected


@pytest.mark.usefixtures("remove_document_file")
def test_write_metadata_document(
dummy_timestamp: datetime,
metadata: DataDocMetadata,
Expand Down Expand Up @@ -134,7 +133,7 @@ def test_write_metadata_document(
assert datadoc_metadata["metadata_last_updated_by"] == PLACEHOLDER_USERNAME


@pytest.mark.usefixtures("existing_metadata_file", "remove_document_file")
@pytest.mark.usefixtures("existing_metadata_file")
def test_write_metadata_document_existing_document(
dummy_timestamp: datetime,
metadata: DataDocMetadata,
Expand All @@ -156,7 +155,6 @@ def test_metadata_id(metadata: DataDocMetadata):
"existing_metadata_path",
[TEST_EXISTING_METADATA_DIRECTORY / "invalid_id_field"],
)
@pytest.mark.usefixtures("remove_document_file")
def test_existing_metadata_none_id(
existing_metadata_file: str,
metadata: DataDocMetadata,
Expand All @@ -175,7 +173,6 @@ def test_existing_metadata_none_id(
"existing_metadata_path",
[TEST_EXISTING_METADATA_DIRECTORY / "valid_id_field"],
)
@pytest.mark.usefixtures("remove_document_file")
def test_existing_metadata_valid_id(
existing_metadata_file: str,
metadata: DataDocMetadata,
Expand Down
1 change: 0 additions & 1 deletion tests/backend/test_model_backwards_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def test_existing_metadata_unknown_model_version():
BACKWARDS_COMPATIBLE_VERSION_DIRECTORIES,
ids=BACKWARDS_COMPATIBLE_VERSION_NAMES,
)
@pytest.mark.usefixtures("remove_document_file")
def test_backwards_compatibility(
existing_metadata_file: str,
metadata: DataDocMetadata,
Expand Down
8 changes: 2 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Shared fixtures and configuration."""

import shutil
import traceback
from collections.abc import Generator
from datetime import datetime
from datetime import timezone
Expand Down Expand Up @@ -43,15 +42,12 @@ def metadata(_mock_timestamp: None) -> DataDocMetadata:
return DataDocMetadata(str(TEST_PARQUET_FILEPATH))


@pytest.fixture()
@pytest.fixture(autouse=True)
def remove_document_file() -> Generator[None, None, None]:
# Yield so we only run teardown
yield None
try:
if TEST_RESOURCES_METADATA_DOCUMENT.exists():
TEST_RESOURCES_METADATA_DOCUMENT.unlink()
except FileNotFoundError as e:
print("File not deleted on teardown, exception caught:") # noqa: T201
traceback.print_exception(type(e), e)


@pytest.fixture()
Expand Down

0 comments on commit d2fa8c8

Please sign in to comment.