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

时间:2021-06-11 11:57:40

标签: python selenium pytest pytest-xdist

当我用

运行下面的代码时

pytest .\tests\GET\test_setupFunc.py

完美运行 即 setup(my_fixture) 函数运行一次,然后 test_one 和 test_two

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

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

失败

我认为 my_fixture 正在被两个测试调用,因为我已经设置了 scope="session" 该函数应该被共享。

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

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

ERROR tests\GET\test_setupFunc.py::test_one - selenium.common.exceptions.WebDriverException:消息:未知错误: 无法删除旧的 devtools 端口文件。 也许 t...

如果您打算运行它,请在添加参数代码步骤中将 yourusername 替换为您的系统用户名

'''

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 相关步骤并只保留打印语句,它可以正常工作

预期结果

  1. setup 方法被调用一次 一旦 setup 成功运行一次,应该调用 2 个方法

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

0 个答案:

没有答案
相关问题