Skip to content

Commit

Permalink
/tests folder : updated with latest changes from python_project_template
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaasRostock committed Nov 5, 2024
1 parent c5fcd0f commit 43e14a8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 62 deletions.
2 changes: 2 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
temp_*
~$temp*
Empty file added tests/__init__.py
Empty file.
142 changes: 80 additions & 62 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,80 @@
import logging
import os
from glob import glob
from pathlib import Path
from shutil import rmtree

import pytest
from pytest import LogCaptureFixture


@pytest.fixture(scope="package", autouse=True)
def chdir():
os.chdir(Path(__file__).parent.absolute() / "test_working_directory")


@pytest.fixture(scope="package", autouse=True)
def test_dir():
return Path(__file__).parent.absolute()


output_dirs = [
"results",
]
output_files = [
"*test*.pdf",
]


@pytest.fixture(autouse=True)
def default_setup_and_teardown(caplog: LogCaptureFixture):
_remove_output_dirs_and_files()
yield
_remove_output_dirs_and_files()


def _remove_output_dirs_and_files():
for folder in output_dirs:
rmtree(folder, ignore_errors=True)
for pattern in output_files:
for file in glob(pattern):
file = Path(file)
file.unlink(missing_ok=True)


@pytest.fixture(autouse=True)
def setup_logging(caplog: LogCaptureFixture):
caplog.set_level("INFO")
caplog.clear()


@pytest.fixture(autouse=True)
def logger():
return logging.getLogger()


def pytest_addoption(parser):
parser.addoption("--show", action="store", default=False)


@pytest.fixture(scope="session")
def show(request):
return request.config.getoption("--show") == "True"
import logging
import os
from pathlib import Path
from shutil import rmtree

import pytest


@pytest.fixture(scope="package", autouse=True)
def chdir() -> None:
"""
Fixture that changes the current working directory to the 'test_working_directory' folder.
This fixture is automatically used for the entire package.
"""
os.chdir(Path(__file__).parent.absolute() / "test_working_directory")


@pytest.fixture(scope="package", autouse=True)
def test_dir() -> Path:
"""
Fixture that returns the absolute path of the directory containing the current file.
This fixture is automatically used for the entire package.
"""
return Path(__file__).parent.absolute()


output_dirs = [
"results",
]
output_files = [
"*test*.pdf",
]


@pytest.fixture(autouse=True)
def default_setup_and_teardown():
"""
Fixture that performs setup and teardown actions before and after each test function.
It removes the output directories and files specified in 'output_dirs' and 'output_files' lists.
"""
_remove_output_dirs_and_files()
yield
_remove_output_dirs_and_files()


def _remove_output_dirs_and_files() -> None:
"""
Helper function that removes the output directories and files specified in 'output_dirs' and 'output_files' lists.
"""
for folder in output_dirs:
rmtree(folder, ignore_errors=True)
for pattern in output_files:
for file in Path.cwd().glob(pattern):
_file = Path(file)
_file.unlink(missing_ok=True)


@pytest.fixture(autouse=True)
def setup_logging(caplog: pytest.LogCaptureFixture) -> None:
"""
Fixture that sets up logging for each test function.
It sets the log level to 'INFO' and clears the log capture.
"""
caplog.set_level("INFO")
caplog.clear()


@pytest.fixture(autouse=True)
def logger() -> logging.Logger:
"""Fixture that returns the logger object."""
return logging.getLogger()


def pytest_addoption(parser):
parser.addoption("--show", action="store", default=False)


@pytest.fixture(scope="session")
def show(request):
return request.config.getoption("--show") == "True"

0 comments on commit 43e14a8

Please sign in to comment.