I have many tests for a class with a C extension. I redo the same tests for:
- the c implementation
- the pure py implementation
- the subclass of the c implementation
- the subclass of the py implementation
Since not all tests are common for the 4 cases, I created 4 files:
- test.py
- test_c.py
- test_subclass.py
- test_subclass_c.py
and a common.py
file filled with pytest.fixture
s and test_*
functions.
To call the same tests for the different classes, I exec
the common.py
. This is an example with test.py
import pytest
import pippo as pippo_module
from pippo.purepy import pippo as pippo_class
from pathlib import Path
c_ext = False
curr_path = Path(__file__)
curr_dir = curr_path.parent
with open(curr_dir / "common.py") as f:
common_code = f.read()
exec(common_code)
with open(curr_dir / "pippo_only.py") as f:
pippo_only_code = f.read()
exec(pippo_only_code)
pippo_only.py
contains tests for only the pure py implementation.
It works, but I feel it really hacky. Do you know if there’s a more simple and elegant way to achive this?