列表理解中的测试和断言
testing and assertion in list comprehension
我是 python 测试的新手,如果可能的话,我想使用 pytest 来检查我的功能是否正确。有一个输入和预期输出列表:
test_cases = [
("...Guide: From Mid .3ms", [(1300000)]),
("OFFERS OVER ,100,000", [(1100000)]),
("...Around .35million", [(1350000)]),
("Guide above .2m", [(1200000)]),
("...From .55 Million", [(2550000)]),
("Low millions", [(2000000)]),
("Mid M's Buyers", [(2000000)]),
("5,000 - 9,950", [(305000), (349950)]),
("...5,000 and 0,000", [(485000), (510000)]),
("...High 0,000's", [(300000)]),
("...000 + to 5,000 +", [(625000)]),
("9k", [(299000)]),
("... Buyers Guide .29M+", [(1290000)]),
("m", [(1000000)]),
(",000,000.00", [(1000000)])
]
如果将 test_cases[n][0]
作为输入,测试我的函数 returns test_cases[n][1]
的最优雅方法是什么?我能否以某种方式断言这一点,同时仍然获得有意义的结果(即 10 次测试中有 7 次成功完成,10 次测试中有 10 次成功完成)?
The parametrize
decorator 这样做。您给它一个输入列表,它将 运行 对输入列表的每个元素进行一次修饰测试。每一个都将被报告为一个单独的测试。
import pytest
test_cases = [
("...Guide: From Mid .3ms", [(1300000)]),
("OFFERS OVER ,100,000", [(1100000)]),
("...Around .35million", [(1350000)]),
("Guide above .2m", [(1200000)]),
("...From .55 Million", [(2550000)]),
("Low millions", [(2000000)]),
("Mid M's Buyers", [(2000000)]),
("5,000 - 9,950", [(305000), (349950)]),
("...5,000 and 0,000", [(485000), (510000)]),
("...High 0,000's", [(300000)]),
("...000 + to 5,000 +", [(625000)]),
("9k", [(299000)]),
("... Buyers Guide .29M+", [(1290000)]),
("m", [(1000000)]),
(",000,000.00", [(1000000)])
]
@pytest.mark.parametrize("in, out", test_cases)
def test(in, out):
assert f(in) == out
reduce
功能类似于其他函数式语言中的 fold
。这是对问题的功能性处理:
from functools import reduce
from operator import and_
def test():
assert reduce(and_, [f(x) == y for x, y in test_cases])
我是 python 测试的新手,如果可能的话,我想使用 pytest 来检查我的功能是否正确。有一个输入和预期输出列表:
test_cases = [
("...Guide: From Mid .3ms", [(1300000)]),
("OFFERS OVER ,100,000", [(1100000)]),
("...Around .35million", [(1350000)]),
("Guide above .2m", [(1200000)]),
("...From .55 Million", [(2550000)]),
("Low millions", [(2000000)]),
("Mid M's Buyers", [(2000000)]),
("5,000 - 9,950", [(305000), (349950)]),
("...5,000 and 0,000", [(485000), (510000)]),
("...High 0,000's", [(300000)]),
("...000 + to 5,000 +", [(625000)]),
("9k", [(299000)]),
("... Buyers Guide .29M+", [(1290000)]),
("m", [(1000000)]),
(",000,000.00", [(1000000)])
]
如果将 test_cases[n][0]
作为输入,测试我的函数 returns test_cases[n][1]
的最优雅方法是什么?我能否以某种方式断言这一点,同时仍然获得有意义的结果(即 10 次测试中有 7 次成功完成,10 次测试中有 10 次成功完成)?
The parametrize
decorator 这样做。您给它一个输入列表,它将 运行 对输入列表的每个元素进行一次修饰测试。每一个都将被报告为一个单独的测试。
import pytest
test_cases = [
("...Guide: From Mid .3ms", [(1300000)]),
("OFFERS OVER ,100,000", [(1100000)]),
("...Around .35million", [(1350000)]),
("Guide above .2m", [(1200000)]),
("...From .55 Million", [(2550000)]),
("Low millions", [(2000000)]),
("Mid M's Buyers", [(2000000)]),
("5,000 - 9,950", [(305000), (349950)]),
("...5,000 and 0,000", [(485000), (510000)]),
("...High 0,000's", [(300000)]),
("...000 + to 5,000 +", [(625000)]),
("9k", [(299000)]),
("... Buyers Guide .29M+", [(1290000)]),
("m", [(1000000)]),
(",000,000.00", [(1000000)])
]
@pytest.mark.parametrize("in, out", test_cases)
def test(in, out):
assert f(in) == out
reduce
功能类似于其他函数式语言中的 fold
。这是对问题的功能性处理:
from functools import reduce
from operator import and_
def test():
assert reduce(and_, [f(x) == y for x, y in test_cases])