美文网首页
pytest-帮助你写出更好的程序代码

pytest-帮助你写出更好的程序代码

作者: 追梦人在路上不断追寻 | 来源:发表于2022-07-22 21:10 被阅读0次

pytest是一款python测试包,它可以非常轻松地写一些小的,可读的,可扩展的应用代码测试。

Intermediate-Advanced-PyTest-Features_Watermarked.43fb169e7121.jpg

安装条件

pytest requires: Python 3.7+ or PyPy3.

安装

pip install -U pytest

简单案例

# content of test_sample.py
def inc(x):
    return x + 1


def test_answer():
    assert inc(3) == 5

执行方法

pytest test_mod.py
pytest testing/
pytest -k "MyClass and not method"
pytest test_mod.py::test_func
pytest test_mod.py::TestClass::test_method

测试类多个测试方法

# content of test_class.py
class TestClass:
    def test_one(self):
        x = "this"
        assert "h" in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, "check")

相关文章

网友评论

      本文标题:pytest-帮助你写出更好的程序代码

      本文链接:https://www.haomeiwen.com/subject/zhkvirtx.html