Ошибка Pytest diff для разметки проекта с несколькими файлами - PullRequest
0 голосов
/ 22 марта 2020

Pytest (pytest -vv ./testing.py) не может показать разницу с импортируемыми объектами (электронными таблицами и функциями). Любые идеи о том, как заставить его работать (как, например, показывать последовательные различия в примерах test_one и test_two) при сохранении макета? Первый тест опускает diff, что наверняка является нежелательным поведением.

Макет проекта:

pytest_so
├── src
│   ├── function.py
│   └── spreadsheets
│       └── sheet.py
└── testing.py

. / Testing.py

from src.spreadsheets.sheet import sheet
from src.function import function

def test_one():
    function(sheet)

def test_two():
    assert {'x': 1, 'y': 2} == {'x': 1, 'y': 3}

. / Src / function .py

def function(sheet):
    assert sheet[0] == sheet[1]

. / src / электронные таблицы / sheet.py

sheet = [
    {'a': 1, 'b': 2},
    {'a': 1, 'b': 3}
]

Вывод:

====================================== FAILURES ======================================
______________________________________ test_one ______________________________________

    def test_one():
>       function(sheet)

pytest_so/testing.py:5: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

sheet = [{'a': 1, 'b': 2}, {'a': 1, 'b': 3}]

    def function(sheet):
>       assert sheet[0] == sheet[1]
E       AssertionError

pytest_so/src/function.py:2: AssertionError
______________________________________ test_two ______________________________________

    def test_two():
>       assert {'x': 1, 'y': 2} == {'x': 1, 'y': 3}
E       AssertionError: assert left == right failed.
E         Showing split diff:
E         
E         left:  {'x': 1, 'y': 2}
E         right: {'x': 1, 'y': 3}
E         
E         1 items in left, but not right:
E         + ('y', 2)
E         1 items in right, but not left:
E         - ('y', 3)

pytest_so/testing.py:8: AssertionError
============================== short test summary info ===============================
FAILED pytest_so/testing.py::test_one - AssertionError
FAILED pytest_so/testing.py::test_two - AssertionError: assert left == right failed.
================================= 2 failed in 0.08s ==================================

1 Ответ

0 голосов
/ 22 марта 2020

Чтобы pytest распознал тестовые файлы, которые не соответствуют обычному шаблону, вы должны настроить их. Создайте pytest.ini в вашем root dir и добавьте:

[pytest]
python_files = src/function.py

Это даст вывод:

================================== FAILURES ===================================
____________________________________ test_two _________________________________

    def test_two():
        s = [{'a': 1, 'b': 2}, {'a': 1, 'b': 3}]
>       cmp_sheet(s)

assert_out.py:18:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

sheet = [{'a': 1, 'b': 2}, {'a': 1, 'b': 3}]

    def cmp_sheet(sheet):
>       assert sheet[0] == sheet[1]
E       AssertionError: assert {'a': 1, 'b': 2} == {'a': 1, 'b': 3}
E         Common items:
E         {'a': 1}
E         Differing items:
E         {'b': 2} != {'b': 3}
E         Full diff:
E         - {'a': 1, 'b': 2}
E         ?               ^
E         + {'a': 1, 'b': 3}
E         ?               ^

src\function.py:2: AssertionError
========================== 1 failed in 0.17 seconds ===========================
...