Selenium Pytest ValueError:设置未产生值

问题描述 投票:0回答:1

我有一个名为“test_invoice.py”的测试套件(使用selenium python pytest),其中有几个测试。 同样,我在其他名为“test_admin.py”、“test_cash.py”等的 python 文件中还有其他测试套件,具有相同的设置。我从这样的 docker 镜像运行所有这些套件:

python -m pytest -v -s --capture=tee-sys --html=report.html --self-contained-html test_invoice.py test_admin.py test_cash.py


@pytest.fixture(scope='class')
def setup(request):
  try:
    options = webdriver.ChromeOptions()
    options.add_argument("--window-size=1920,1080")
    options.add_argument("--start-maximized")
    options.add_argument('--headless')
    options.add_argument('--ignore-certificate-errors')
    driver = webdriver.Chrome(options=options)
    request.cls.driver = driver
    driver.delete_all_cookies()
    driver.get(TestData_common.BASE_URL)
    yield
    driver.quit()
except WebDriverException as e:
    print('App seems to be down...> ', e)


@pytest.mark.usefixtures("setup")
@pytest.mark.incremental
class Test_app:
  def test_001_login(self):
    assert TestData_common.URL_FOUND, "UAT seems to be down.."
    self.loginPage = LoginPage(self.driver)
    self.loginPage.do_click_agree_button()
    assert TestData_common.AGREE_BTN_FOUND, "Unable to click AGREE button.."
    self.driver.maximize_window()
    print('Successfully clicked AGREE button')
    time.sleep(2)

问题:有时我会收到以下错误

request = <SubRequest 'setup' for <Function test_001_login>>
kwargs = {'request': <SubRequest 'setup' for <Function test_001_login>>}

def call_fixture_func(
    fixturefunc: "_FixtureFunc[FixtureValue]", request: FixtureRequest, kwargs
) -> FixtureValue:
    if is_generator(fixturefunc):
        fixturefunc = cast(
            Callable[..., Generator[FixtureValue, None, None]], fixturefunc
        )
        generator = fixturefunc(**kwargs)
        try:
            fixture_result = next(generator)
        except StopIteration:
>               raise ValueError(f"{request.fixturename} did not yield a value") from 
None
E               **ValueError: setup did not yield a value**

我不知道为什么会发生这种情况。 pytest 夹具有问题吗? 非常感谢任何帮助。

python selenium-webdriver pytest
1个回答
0
投票

错误消息“setup did not generated a value”表示设置夹具函数没有按预期产生值。

Python中的yield关键字在函数中使用,类似于return语句,但它返回一个生成器。在 pytest 装置的上下文中,yield 用于为测试代码提供设置-拆卸机制。在yield语句之前的代码是setup部分,在yield之后的代码是teardown部分。

在您的情况下,yield 语句位于 try 块内。如果在yield语句之前发生异常,则将无法达到yield,并且pytest将引发ValueError,因为它期望fixture产生一个值。

要解决这个问题,您应该确保始终到达yield 语句。您可以通过将yield 语句移到try 块之外来完成此操作。 所以,这可能对你有用 -

@pytest.fixture(scope='class')
def setup(request):
    driver = None
    try:
        options = webdriver.ChromeOptions()
        options.add_argument("--window-size=1920,1080")
        options.add_argument("--start-maximized")
        options.add_argument('--headless')
        options.add_argument('--ignore-certificate-errors')
        driver = webdriver.Chrome(options=options)
        request.cls.driver = driver
        driver.delete_all_cookies()
        driver.get(TestData_common.BASE_URL)
    except WebDriverException as e:
        print('App seems to be down...> ', e)
    yield driver
    if driver is not None:
        driver.quit()
© www.soinside.com 2019 - 2024. All rights reserved.