You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importpytest@pytest.mark.user@pytest.mark.roledefxxx():
pass@pytest.mark.user@pytest.mark.rolesdefyyy():
pass-m的筛选字符串可以写成下面的样子:
"user""user or roles""user and role""user and not role"
importpytest@pytest.fixture(scope='class')defclass_scope():
"""A class scope fixture."""print("你好")
yieldprint("结束")
@pytest.mark.usefixtures('class_scope')classTestSomething():
"""Demo class scope fixtures."""deftest_1(self):
"""Test using a class scope fixture."""deftest_2(self):
"""Again, multiple tests are more fun."""
习惯上总是使用 autouse
"""Demonstrate autouse fixtures."""importpytestimporttime@pytest.fixture(autouse=True, scope='session')deffooter_session_scope():
"""Report the time at the end of a session."""yieldnow=time.time()
print('--')
print('finished : {}'.format(time.strftime('%d %b %X', time.localtime(now))))
print('-----------------')
@pytest.fixture(autouse=True)deffooter_function_scope():
"""Report test durations after each function."""start=time.time()
yieldstop=time.time()
delta=stop-startprint('\ntest duration : {:0.3} seconds'.format(delta))
deftest_1():
"""Simulate long-ish running test."""time.sleep(1)
deftest_2():
"""Simulate slightly longer test."""time.sleep(1.23)
可以使用autouse=True来使这个fixture被自动的使用,和直接写相比,不能够传入参数了.
如果这样写十分的有必要不然的话,建议是直接写名字
pytest 笔记
使用 assert 进行断言
使用 pytest file1.py file2.py 去测试一个单独的文件
使用 pytest filepath 测试这个文件夹下的所有测试文件
使用 pytest 测试当前文件夹下的所有测试文件
格式
pytest 的输出
@pytest.mark.ifskip()
如果成功就是 XPASS(X).
测试一个测试文件中的一个测试例 pytest test_something.py::test_nihao
命令行参数
pytest Fixtures
测试文件夹的根目录,当然其他子目录也可以,但是根目录会更加的有意义. pytest
会自动的找 这个文件所以不用在测试文件中导入这个模块
通常情况下我们使用函数名当作 fixture 的名字,也可是使用 @pytest.fixture(name="lue") 这样来指定 fixture 的名字.
The text was updated successfully, but these errors were encountered: