Hug*_*ins 32 python selenium pytest
我正在编写selenium测试,包含一组类,每个类包含几个测试.每个类当前打开然后关闭Firefox,这有两个结果:
我可以通过添加睡眠来解决错误54,但它仍然会超级慢.
所以,我想要做的是在所有测试类中重用相同的Firefox实例.这意味着我需要在所有测试类之前运行一个方法,在所有测试类之后运行另一个方法.因此,'setup_class'和'teardown_class'是不够的.
dra*_*nHR 47
使用hpk42建议的会话夹具在很多情况下都可以使用,但夹具只有在收集完所有测试后才能运行.如果您想在测试集合之前运行代码,那么您使用的文档很少pytest_configure
或者pytest_sessionstart
:
# content of conftest.py
def pytest_configure(config):
"""
Allows plugins and conftest files to perform initial configuration.
This hook is called for every plugin and initial conftest
file after command line options have been parsed.
"""
def pytest_sessionstart(session):
"""
Called after the Session object has been created and
before performing collection and entering the run test loop.
"""
def pytest_sessionfinish(session, exitstatus):
"""
Called after whole test run finished, right before
returning the exit status to the system.
"""
def pytest_unconfigure(config):
"""
called before test process is exited.
"""
Run Code Online (Sandbox Code Playgroud)
hpk*_*k42 40
您可能想要使用会话范围的"autouse"夹具:
# content of conftest.py or a tests file (e.g. in your tests or root directory)
@pytest.fixture(scope="session", autouse=True)
def do_something(request):
# prepare something ahead of all tests
request.addfinalizer(finalizer_function)
Run Code Online (Sandbox Code Playgroud)
这将在所有测试之前运行.最终测试完成后将调用终结器.
Pav*_*voy 13
从版本2.10开始,有一种更简洁的方法来拆除夹具并定义其范围.所以你可以使用这个语法:
@pytest.fixture(scope="module", autouse=True)
def my_fixture():
print ('INITIALIZATION')
yield param
print ('TEAR DOWN')
Run Code Online (Sandbox Code Playgroud)
autouse参数: 来自文档:
以下是autouse灯具在其他范围内的工作原理:
autouse fixtures服从scope = keyword-argument:如果autouse fixture具有scope ='session',它将只运行一次,无论它在何处定义.scope ='class'表示每个类都会运行一次,等等.
如果在测试模块中定义了autouse fixture,则其所有测试函数都会自动使用它.
如果在conftest.py文件中定义了autouse fixture,那么其目录下所有测试模块中的所有测试都将调用fixture.
...
"请求"参数: 请注意,"request"参数对于您的目的不是必需的,尽管您可能希望将其用于其他目的.来自文档:
"Fixture函数可以接受请求对象来内省"请求"测试函数,类或模块上下文."
小智 6
尝试pytest_sessionstart(session)
在conftest.py
例子:
# project/tests/conftest.py
def pytest_sessionstart(session):
print('BEFORE')
Run Code Online (Sandbox Code Playgroud)
# project/tests/tests_example/test_sessionstart.py
import pytest
@pytest.fixture(scope='module', autouse=True)
def fixture():
print('FIXTURE')
def test_sessonstart():
print('TEST')
Run Code Online (Sandbox Code Playgroud)
日志:
BEFORE
============================================================================ test session starts =============================================================================
platform darwin -- Python 3.7.0, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 -- /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
cachedir: .pytest_cache
rootdir: /Users/user/Documents/test, inifile: pytest.ini
plugins: allure-pytest-2.8.12, env-0.6.2
collected 1 item
tests/6.1/test_sessionstart.py::test_sessonstart FIXTURE
TEST
PASSED
Run Code Online (Sandbox Code Playgroud)
在此处阅读更多信息:https : //docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_sessionstart