是否有一个最近的pytest替代与简单"断言"?

时间:2017-08-14 16:03:02

标签: python pytest

我想使用pytest这样的东西进行非常基本的测试,使用简单的断言。 pytest是最好的选择吗?还是有更好的近期选择?

4 个答案:

答案 0 :(得分:3)

据我所知,py.test仍然是生意!

答案 1 :(得分:1)

pytest非常适合简单测试。

def foo():
    return (1,2,3)

def test_foo():
    assert foo() == (1,2,3)

不确定如何使这更简单。

答案 2 :(得分:1)

两个方面- 就实际的替代测试库而言,pytest几乎是行业标准,没有明确的竞争对手。我找不到其他使用“简单assert”样式api的测试框架。

现在,如果您要寻找比pytest还要简约的商品,assert实际上是python builtin,则可以编写自己的微型跑步机:

def run_tests(**test_fns):
     failures = dict()
     for test_name, test_fn in test_fns.items():
         try:
             test_fn()
         except Exception as e:
             failures[test_name] = e
     return failures

def failing_test():
     assert False, 'failing test failed'

run_tests(fail=failing_test)
#> {'fail': AssertionError('failing test failed')}
# then maybe pretty print the failures and exit properly

如果您想避免标准库中包含的第三方库,也可以考虑使用doctest

答案 3 :(得分:1)

您可以使用htf创建非常简单的测试。它是HILSTER TestBench的一部分,可以通过社区许可证免费使用。

import htf

def test_example():
    assert False, "This assertions failed!"

def test_another_example(assertions):
    assertions.assert_true(False, "This assertions failed!")

if __name__ == "__main__":
    htf.main()