def test_dGetBlacklist(self): with AccountManager(ctx=self.ctx, account=DefaultPassenger), AccountManager(ctx=self.ctx,account=FastDriver):怎么通过@pytest.fixture实现执行用例时替换DefaultPassenger和FastDriver
时间: 2024-02-13 17:59:39 浏览: 26
可以使用 pytest 的 fixture 功能来替换 `DefaultPassenger` 和 `FastDriver`。首先,在 conftest.py 文件中定义一个 fixture 函数,该函数返回一个包含替换后账号的元组。
```python
import pytest
from AccountManager import AccountManager
@pytest.fixture
def replace_accounts():
return (AccountManager(ctx=self.ctx, account='replaced_Passenger'),
AccountManager(ctx=self.ctx, account='replaced_Driver'))
```
然后,在测试用例中使用这个 fixture 函数作为参数,将替换后的账号传递给测试用例。
```python
def test_dGetBlacklist(self, replace_accounts):
with replace_accounts[0], replace_accounts[1]:
# 执行测试用例
pass
```
这样,在执行测试用例时,pytest 将自动调用 `replace_accounts` 函数来替换 `DefaultPassenger` 和 `FastDriver`。
相关问题
命令行输入测试用例、乘客账号、司机账号,怎么通过@pytest.fixture实现执行是替换参数
可以使用 pytest 的 fixture 功能和命令行参数插件 `pytest-cmdline-parser` 来实现。首先,在 conftest.py 文件中定义一个 fixture 函数,该函数接收命令行参数并返回一个包含替换后账号的元组。
```python
import pytest
from AccountManager import AccountManager
def pytest_addoption(parser):
parser.addoption('--passenger', action='store', help='passenger account')
parser.addoption('--driver', action='store', help='driver account')
@pytest.fixture
def replace_accounts(request):
passenger = request.config.getoption('--passenger')
driver = request.config.getoption('--driver')
return (AccountManager(ctx=self.ctx, account=passenger),
AccountManager(ctx=self.ctx, account=driver))
```
然后,在测试用例中使用这个 fixture 函数作为参数,将替换后的账号传递给测试用例。在命令行中输入要替换的账号。
```python
def test_dGetBlacklist(self, replace_accounts):
with replace_accounts[0], replace_accounts[1]:
# 执行测试用例
pass
```
在命令行中输入以下命令来执行测试用例并替换账号:
```
pytest --passenger=your_passenger_account --driver=your_driver_account
```
这样,在执行测试用例时,pytest 将自动调用 `replace_accounts` 函数来替换账号参数。
pytest selenium 并发执行多条用例,每个用例都需要登录账号,账号不能同时登录,用例执行完毕后需要归还,保证下一个用例有账号使用,使用list管理账号。
您可以使用 pytest 的 `pytest-xdist` 插件来实现并发执行多条用例。同时,您可以使用一个账号管理类来控制账号的登录和归还。
首先,安装 `pytest-xdist` 插件:
```shell
pip install pytest-xdist
```
然后,创建一个账号管理类,例如 `AccountManager`:
```python
class AccountManager:
def __init__(self, accounts):
self.accounts = accounts
self.used_accounts = []
def get_account(self):
account = self.accounts.pop(0)
self.used_accounts.append(account)
return account
def release_account(self, account):
self.used_accounts.remove(account)
self.accounts.append(account)
```
在测试文件中,您可以使用 `pytest.fixture` 来创建一个共享的账号管理实例,并在测试用例中使用它来获取和释放账号:
```python
import pytest
@pytest.fixture(scope="session")
def account_manager():
# 创建账号列表
accounts = ["account1", "account2", "account3"]
# 创建账号管理实例
manager = AccountManager(accounts)
yield manager
# 测试会话结束后,归还所有账号
for account in manager.used_accounts:
manager.release_account(account)
def test_case(account_manager):
# 获取账号
account = account_manager.get_account()
# 使用账号执行测试逻辑
# ...
# 释放账号
account_manager.release_account(account)
```
接下来,在运行测试时,您可以使用 `pytest-xdist` 插件来指定并发执行的数量。例如,使用 `-n` 参数来指定使用 2 个进程并发执行:
```shell
pytest -n 2
```
这样,每个用例都会获取一个账号进行登录,并在执行完毕后归还账号,保证下一个用例可以继续使用账号。