Skip to main content
Pytest 会自动扫描项目中以 test_开头或以_test结尾的文件,读取文件中 test_ 开头的函数和以 Test 开头的类中的函数,识别为测试用例,自动执行!

测试函数

终端执行pytest, 会自动找到了所有 test_ 开头的函数并执行了它们!
test_demo1.py
def test_add():
    assert 1 + 1 == 2

测试类

当你运行 pytest的时候,TestCalculator 类里的 test_add 函数就会被执行!
test_demo2.py
class TestCalculator:
    def abc(self):
        assert 1 + 1 == 2
    def test_add(self):
        assert 1 + 1 > 2