| 1 |
import pytest |
| 2 |
|
| 3 |
|
| 4 |
def test_foo(datadir, num_regression): |
| 5 |
""" |
| 6 |
Dumb test, just to generate a expected csv file that must be different from the one gathered |
| 7 |
from `TestClass::test_foo`. |
| 8 |
""" |
| 9 |
data = {"ones": [1, 1, 1, 1], "zeros": [0, 0, 0, 0]} |
| 10 |
num_regression.check(data) |
| 11 |
|
| 12 |
expected_filename = f"{test_foo.__name__}.csv" |
| 13 |
obtained_filename = f"{test_foo.__name__}.obtained.csv" |
| 14 |
assert (datadir / expected_filename).exists() |
| 15 |
assert (datadir / obtained_filename).exists() |
| 16 |
|
| 17 |
|
| 18 |
class TestClass: |
| 19 |
def test_foo(self, datadir, num_regression): |
| 20 |
""" |
| 21 |
Since 2.2.1, pytest-regressions use the test class name to compose the name of the datafiles, by default. |
| 22 |
This tests asserts this behavior. |
| 23 |
""" |
| 24 |
data = {"twos": [2, 2, 2, 2], "threes": [3, 3, 3, 3]} |
| 25 |
|
| 26 |
|
| 27 |
num_regression._with_test_class_names = True |
| 28 |
num_regression.check(data) |
| 29 |
|
| 30 |
expected_filename = f"{TestClass.__name__}_{TestClass.test_foo.__name__}.csv" |
| 31 |
obtained_filename = ( |
| 32 |
f"{TestClass.__name__}_{TestClass.test_foo.__name__}.obtained.csv" |
| 33 |
) |
| 34 |
assert (datadir / expected_filename).exists() |
| 35 |
assert (datadir / obtained_filename).exists() |
| 36 |
|
| 37 |
|
| 38 |
class TestClassWithIgnoredName: |
| 39 |
def test_foo(self, datadir, num_regression): |
| 40 |
""" |
| 41 |
Specifies to not use the class name to compose the expected data filename. The filename coincides with the |
| 42 |
expected data filename used by `test_foo` function. The regression test then fails because the expected |
| 43 |
data is different. |
| 44 |
""" |
| 45 |
data = {"fours": [4, 4, 4, 4], "fives": [5, 5, 5, 5]} |
| 46 |
|
| 47 |
|
| 48 |
with pytest.raises(AssertionError): |
| 49 |
num_regression.check(data) |
| 50 |
|
| 51 |
|
| 52 |
expected_filename = f"{TestClassWithIgnoredName.test_foo.__name__}.csv" |
| 53 |
obtained_filename = f"{TestClassWithIgnoredName.test_foo.__name__}.obtained.csv" |
| 54 |
assert (datadir / expected_filename).exists() |
| 55 |
assert (datadir / obtained_filename).exists() |
| 56 |
|
| 57 |
|
| 58 |
expected_with_class_name = f"{TestClassWithIgnoredName.__name__}_{TestClassWithIgnoredName.test_foo.__name__}.csv" |
| 59 |
obtained_with_class_name = f"{TestClassWithIgnoredName.__name__}_{TestClassWithIgnoredName.test_foo.__name__}.obtained.csv" |
| 60 |
assert not (datadir / expected_with_class_name).exists() |
| 61 |
assert not (datadir / obtained_with_class_name).exists() |