Skip to main content
符合pytest扫描规则代码,它会自动运行,而不用你手动再调用

自动测试函数

pytest 会自动找到了所有 test_ 开头的函数并执行了它们!
def test_add():
    assert 1 + 1 == 2
当你运行 pytest test_demo.py的时候,这个函数就会被执行!

自动测试类

pytest 会自动找并自动执行 TestXxx 开头的 类里的 test_xxx方法
class TestCalculator:
    def abc(self):
        assert 1 + 1 == 2
    def test_add(self):
        assert 1 + 1 > 2
当你运行 pytest test_demo.py的时候,这个TestCalculator类里的test_add函数就会被执行!