【发布时间】:2022-01-04 04:09:50
【问题描述】:
我是 pytest 新手,下面是我的代码。我正在使用断言。断言工作正确,这是错误的,因为数据不匹配。我需要帮助来捕获哪些数据不匹配。
def test_content_database():
records_src = [(1,James,smith,123),(2,Granpa,smith,124),(3,Linda,smith,123)]
records_trg = [(1,James,smith,**124**),(2,Granpa,**ron**,124),(3,Linda,smith,123)]
print("ASSERTION RESULTED IN : ", all([a == b for a, b in zip(records_src, records_trg)]))
assert all([a == b for a, b in zip(records_src, records_trg)]), "DATA IS NOT MATCHING"
**控制台输出是错误的,这是正确的,但我想捕获哪些数据不匹配**
main_test.py:8 (test_content_database)
def test_content_database():
records_src = dbcon.get_data_connection_src()
records_trg = dbcon.get_data_connection_trg_diff()
print("ASSERTION RESULTED IN : ", all([a == b for a, b in zip(records_src, records_trg)]))
> assert all([a == b for a, b in zip(records_src, records_trg)]), "DATA IS NOT MATCHING"
E AssertionError: DATA IS NOT MATCHING
E assert False
E + where False = all([False, False, True, True, True, True, ...])
main_test.py:13: AssertionError
【问题讨论】:
-
你不能。
all本身不会返回比True或False更多的信息。如果您想确切知道哪些元素是False,您将不得不手动遍历列表 -
我建议不要将太多内容打包到单行断言中——你最好有一个包含单个断言的实际循环(然后 pytest 会显示差异)跨度>