pytest+uiautomator2多进程多设备执行
时间: 2023-07-06 12:13:56 浏览: 199
要实现pytest+uiautomator2多进程多设备执行,可以按照以下步骤操作:
1. 安装pytest-xdist插件,可以使用pip install pytest-xdist命令进行安装。
2. 编写pytest.ini配置文件,指定pytest-xdist插件的参数,例如:
```
[pytest]
addopts = -n 3
```
其中,-n参数指定了使用3个进程执行测试用例。
3. 编写conftest.py配置文件,使用pytest_addoption函数添加自定义参数,例如:
```
def pytest_addoption(parser):
parser.addoption('--devices', action='store', default='device1,device2', help='设备列表')
```
其中,--devices参数指定了要执行测试用例的设备列表。
4. 编写pytest fixture,使用pytest.fixture装饰器定义uiautomator2的设备对象,例如:
```
import pytest
import uiautomator2 as u2
@pytest.fixture(scope='module', params=['device1', 'device2'])
def device(request):
device_name = request.param
d = u2.connect(device_name)
return d
```
其中,params参数指定了要执行测试用例的设备列表,fixture名称为device,作用域为module,返回uiautomator2的设备对象。
5. 编写测试用例,使用fixture名称作为参数,例如:
```
def test_example(device):
assert device.app_current()['package'] == 'com.android.settings'
```
其中,device参数为fixture名称,表示传入uiautomator2的设备对象。
6. 执行测试,使用pytest命令执行测试用例,例如:
```
pytest -s -v --devices=device1,device2
```
其中,--devices参数指定了要执行测试用例的设备列表。
这样,就可以实现pytest+uiautomator2多进程多设备执行测试用例了。
阅读全文
相关推荐


















