当通过pytest xdist在python selenium中并行运行时,测试失败,但顺序良好

2024-06-28 11:29:06 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行下面的代码时

pytest .\tests\GET\test_setupFunc.py

它工作得很好 i、 e.设置(我的夹具)功能运行一次,然后测试一和测试二

如果我使用pytest xdist并行运行它

pytest .\tests\GET\test_setupFunc.py -n=2

它失败了

我认为两个测试都在调用我的_fixture,因为我已经设置了scope="session",该函数应该共享

一旦该函数成功运行,我希望并行性应该得到触发器

============短测试摘要信息================

错误测试\GET\test\u setupFunc.py::test\u one-selenium.common.exceptions.WebDriverException:消息:未知错误: 无法删除旧的devtools端口文件。可能

请务必在添加参数代码步骤中将您的用户名替换为系统用户名,以防您计划运行该步骤

'''

import pytest
import logging
logging.basicConfig(format='%(message)s')

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

@pytest.fixture(autouse=True, scope='session')
def my_fixture():
   logging.warning('setup function')
   print(" i will be called only once i am one time")
   options = webdriver.ChromeOptions() 
   options.add_argument('--profile-directory=Default')
   options.add_argument("--user-data-dir=c:\\Users\\yourUserName\\AppData\\Local\\Google\\Chrome\\User Data")
   driver=webdriver.Chrome(ChromeDriverManager().install(), options=options)
   driver.get("https://gmail.com")
   
   driver.quit()

def test_one():
    logging.warning('test_a')

    print(" i am first function")

def test_two():
    logging.warning('test_b')
    print(" i am second function")

还要注意的是,如果我删除了与selenium相关的步骤,只保留print语句,它就可以正常工作

预期结果

  1. 设置方法只调用一次 安装程序成功运行一次后,应调用2个方法

pytest-parallel不支持python 3.9,但也尝试过


Tags: pytestimportgetpytestloggingdefselenium