Python 如何在Pytest测试套件中包含带有init的测试类?

Python 如何在Pytest测试套件中包含带有init的测试类?,python,pytest,Python,Pytest,我正在尝试使用pytest和Selenium创建一个测试套件,使用页面对象模型进行模式设计。为了在测试中使用我的页面类,我只是将它们导入到TestClass\uuuu init\uuuu方法中,因为它们需要用驱动程序实例化 我知道,在默认情况下,pytest会使用\uuuu init\uuuu方法忽略类。通过阅读,我还知道可以配置pytest收集测试的位置。是否有可能让它考虑使用 >第i个ITITSY < /代码>进行类测试,而不是返回“空套件”错误? Pytest和Unittest有一些不同的

我正在尝试使用pytest和Selenium创建一个测试套件,使用页面对象模型进行模式设计。为了在测试中使用我的页面类,我只是将它们导入到TestClass
\uuuu init\uuuu
方法中,因为它们需要用驱动程序实例化

我知道,在默认情况下,pytest会使用
\uuuu init\uuuu
方法忽略类。通过阅读,我还知道可以配置pytest收集测试的位置。是否有可能让它考虑使用<代码> >第i个ITITSY < /代码>进行类测试,而不是返回“空套件”错误?


Pytest和Unittest有一些不同的约定。在同一个测试函数中混合使用这两种方法通常是值得避免的

如果您专门使用Pytest,您将把fixture作为参数传递给测试函数,例如:

import pytest
from selenium import webdriver


@pytest.fixture
def driver():
    driver = webdriver.Chrome()
    return driver


def test_func(driver):
    # `driver` is found by pytest in the fixture above and
    # automatically passed in
    request = ... # Instantiate your request (not in your included code)

    session = request.node
    page = PageFunctions(driver)
    login_page = LoginPage(driver)
    registration_page = RegistrationPage(driver)

    # Make some assertions about your data, e.g.:
    assert page is not None

您还没有包括所有的对象定义/导入,因此很难看到您试图通过测试实现什么,但希望这能让您了解pytest约定。

pytest和Unittest有一些不同的约定。在同一个测试函数中混合使用这两种方法通常是值得避免的

如果您专门使用Pytest,您将把fixture作为参数传递给测试函数,例如:

import pytest
from selenium import webdriver


@pytest.fixture
def driver():
    driver = webdriver.Chrome()
    return driver


def test_func(driver):
    # `driver` is found by pytest in the fixture above and
    # automatically passed in
    request = ... # Instantiate your request (not in your included code)

    session = request.node
    page = PageFunctions(driver)
    login_page = LoginPage(driver)
    registration_page = RegistrationPage(driver)

    # Make some assertions about your data, e.g.:
    assert page is not None

您没有包括所有的对象定义/导入,因此很难看到您试图通过测试实现什么,但希望这能让您了解pytest约定。

您能提供一个源代码示例吗?实际上,我绕过了需要的
\uuu init\uuu
方法,使用我的driver\u init pytest fixture来启动我需要的东西,从而解决了这个问题。作为参考,这是我最后用来启动我需要的类并在我的测试类中将它们用作类属性的代码。你能提供一个源代码的示例吗?我实际上通过绕过需要
\uuuu init\uuuu
方法和使用我的driver\u init pytest fixture来启动我需要的东西来管理解决方法。作为参考,这是我最后用来启动所需类并在测试类中将它们用作类属性的代码