File: test_filenames.py

package info (click to toggle)
pytest-regressions 2.5.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,012 kB
  • sloc: python: 2,367; makefile: 18
file content (61 lines) | stat: -rw-r--r-- 2,641 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import pytest


def test_foo(datadir, num_regression):
    """
    Dumb test, just to generate a expected csv file that must be different from the one gathered
    from `TestClass::test_foo`.
    """
    data = {"ones": [1, 1, 1, 1], "zeros": [0, 0, 0, 0]}
    num_regression.check(data)

    expected_filename = f"{test_foo.__name__}.csv"
    obtained_filename = f"{test_foo.__name__}.obtained.csv"
    assert (datadir / expected_filename).exists()
    assert (datadir / obtained_filename).exists()


class TestClass:
    def test_foo(self, datadir, num_regression):
        """
        Since 2.2.1, pytest-regressions use the test class name to compose the name of the datafiles, by default.
        This tests asserts this behavior.
        """
        data = {"twos": [2, 2, 2, 2], "threes": [3, 3, 3, 3]}

        # equivalent to use "--with-test-class-names" parameter
        num_regression._with_test_class_names = True
        num_regression.check(data)

        expected_filename = f"{TestClass.__name__}_{TestClass.test_foo.__name__}.csv"
        obtained_filename = (
            f"{TestClass.__name__}_{TestClass.test_foo.__name__}.obtained.csv"
        )
        assert (datadir / expected_filename).exists()
        assert (datadir / obtained_filename).exists()


class TestClassWithIgnoredName:
    def test_foo(self, datadir, num_regression):
        """
        Specifies to not use the class name to compose the expected data filename. The filename coincides with the
        expected data filename used by `test_foo` function. The regression test then fails because the expected
        data is different.
        """
        data = {"fours": [4, 4, 4, 4], "fives": [5, 5, 5, 5]}

        # Trying to compare this data with `test_foo` expected data.
        with pytest.raises(AssertionError):
            num_regression.check(data)

        # Assert that we have the expected names, without class name
        expected_filename = f"{TestClassWithIgnoredName.test_foo.__name__}.csv"
        obtained_filename = f"{TestClassWithIgnoredName.test_foo.__name__}.obtained.csv"
        assert (datadir / expected_filename).exists()
        assert (datadir / obtained_filename).exists()

        # Assert that don't have files with class name
        expected_with_class_name = f"{TestClassWithIgnoredName.__name__}_{TestClassWithIgnoredName.test_foo.__name__}.csv"
        obtained_with_class_name = f"{TestClassWithIgnoredName.__name__}_{TestClassWithIgnoredName.test_foo.__name__}.obtained.csv"
        assert not (datadir / expected_with_class_name).exists()
        assert not (datadir / obtained_with_class_name).exists()