编程语言
首页 > 编程语言> > python-列表理解中的测试和断言

python-列表理解中的测试和断言

作者:互联网

我是python测试的新手,如果可能的话,请使用pytest来检查我的函数是否正确.有输入和预期输出的列表:

    test_cases = [
        ("...Guide: From Mid $1.3ms", [(1300000)]),
        ("OFFERS OVER $1,100,000", [(1100000)]),
        ("...Around $1.35million", [(1350000)]),
        ("Guide above $1.2m", [(1200000)]),
        ("...From $2.55 Million", [(2550000)]),
        ("Low $2 millions", [(2000000)]),
        ("Mid $2M's Buyers", [(2000000)]),
        ("$305,000 - $349,950", [(305000), (349950)]),
        ("...$485,000 and $510,000", [(485000), (510000)]),
        ("...High $300,000's", [(300000)]),
        ("...000 + to $625,000 +", [(625000)]),
        ("$299k", [(299000)]),
        ("... Buyers Guide $1.29M+", [(1290000)]),
        ("$1m", [(1000000)]),
        ("$1,000,000.00", [(1000000)])
        ]

如果给出test_cases [n] [0]作为输入,测试我的函数是否返回test_cases [n] [1]的最优雅方法是什么?我能否以某种方式断言这一点,同时仍然获得有意义的结果(即,每10个测试中有7个成功完成,每10个测试中有10个成功完成)?

解决方法:

The parametrize decorator会这样做.您给它一个输入列表,它将对输入列表的每个元素运行一次经过修饰的测试.每个测试都会作为单独的测试报告.

import pytest

test_cases = [
    ("...Guide: From Mid $1.3ms", [(1300000)]),
    ("OFFERS OVER $1,100,000", [(1100000)]),
    ("...Around $1.35million", [(1350000)]),
    ("Guide above $1.2m", [(1200000)]),
    ("...From $2.55 Million", [(2550000)]),
    ("Low $2 millions", [(2000000)]),
    ("Mid $2M's Buyers", [(2000000)]),
    ("$305,000 - $349,950", [(305000), (349950)]),
    ("...$485,000 and $510,000", [(485000), (510000)]),
    ("...High $300,000's", [(300000)]),
    ("...000 + to $625,000 +", [(625000)]),
    ("$299k", [(299000)]),
    ("... Buyers Guide $1.29M+", [(1290000)]),
    ("$1m", [(1000000)]),
    ("$1,000,000.00", [(1000000)])
]

@pytest.mark.parametrize("in", "out", test_cases)
def test(in, out):
    assert f(in) == out

标签:unit-testing,testing,automated-tests,tdd,python
来源: https://codeday.me/bug/20191027/1943720.html