Pytest(pytest -vv ./testing.py
(无法显示导入对象(电子表格和函数(的差异。关于如何在保留布局的同时使其工作(好像在示例test_one
和test_two
之间显示一致的差异(,有什么想法吗?第一个测试是省略diff,这肯定是不希望的行为。
项目布局:
pytest_so
├── src
│ ├── function.py
│ └── spreadsheets
│ └── sheet.py
└── testing.py
/测试.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 ==================================
为了让pytest识别不遵循通常模式的测试文件,您必须对它们进行配置。在根目录中创建一个pytest.ini
并添加:
[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 ? ^
srcfunction.py:2: AssertionError
========================== 1 failed in 0.17 seconds ===========================