【问题标题】:Python Selenium run multiple test classes in one selenium webdriver using with pytestPython Selenium 使用 pytest 在一个 selenium webdriver 中运行多个测试类
【发布时间】:2018-04-05 10:57:49
【问题描述】:

我是 python 新手,我开始创建一个关于 GUI 的自动化测试套件(不同文件中的多个测试用例)。我想在一个 selenium webdriver 中执行我的所有测试用例,所以我创建了一个单例 webdriver 类,我想在我的所有测试用例中使用这个类。这是我的单例 webdriver 类:

from selenium import webdriver


class Webdriver(object):
    """
    Singleton class based on overriding the __new__ method
    """

    def __new__(cls):
        """
        Override __new__ method to control the obj. creation
        :return: Singleton obj.
        """
        if not hasattr(cls, 'instance'):
            cls.instance = super(Webdriver, cls).__new__(cls)

        return cls.instance

    @staticmethod
    def get_driver():
        """
        :return: Selenium driver
        """
        dir = "C:\\Python36\\Lib\\site-packages\\selenium\\webdriver\\ie"
        ie_driver_path = dir + "\\IEDriverServer.exe"
        return webdriver.Ie(ie_driver_path)

还有我的设置示例:

from Core.Webdriver import Webdriver

class LoginExternal(unittest.TestCase):

    def setUp(self):
        # Create Instances
        self.web = Webdriver.get_driver()
        self.matcher = TemplateMatcher()
        self.gif = GifCapture("C:\\wifi\\Videos\\" + self._testMethodName + ".gif")
        self.gif.start()
        time.sleep(3)

     def test_LoginExternal(self):
         # Open External Login Page
         self.web.maximize_window()

这是我的问题,当我执行我的测试套件时,我的代码会创建一个新的 selenium 实例,但我希望在所有测试用例中只使用一个 selenium 实例。

我使用 pytest 作为测试运行器,使用以下 cmd 命令:

pytest --pyargs --html=Report/External_04052018.html ExternalTestSuite/

我认为问题在于 pytest 在每个测试用例执行中都使用了一个新进程。有没有办法防止或使用这种方式?

【问题讨论】:

  • 如果您使用pytest,为什么不使用fixture 来创建selenium web 驱动程序?
  • 我不知道在这种情况下如何使用 pytest 夹具。你能给我举个小例子吗?
  • 昨晚我搜索了 pytest 夹具,发现:“unittest.TestCase 子类不支持夹具 - 如果您想使用 pytest 功能,请使用没有任何继承的类(或在 Python 2 上继承对象) ).",所以我认为 pytest 夹具不是我的解决方案。

标签: python selenium selenium-webdriver pytest pytest-html


【解决方案1】:

与传统的 XUnit 系列测试运行器相比,Pytest 最大的特性和优势在于它具有固定装置。我邀请你使用它。在您的场景中,我将摆脱扩展 unittest.TestCasesetUp 方法以支持 pytest 夹具,如下所示:

import pytest

from Core.Webdriver import Webdriver


class TestLoginExternal:

    @pytest.fixture(scope='class')
    def driver(self):
        print('Setup runs once before all tests in class')
        driver = Webdriver.get_driver()
        yield driver
        driver.quit()
        print('Teardown runs once after all tests in class')

    def test_LoginExternal(self, driver):
        # Open External Login Page
        print('test_LoginExternal')

    def test_LoginInternal(self, driver):
        # Open External Login Page
        print('test_LoginInternal')

【讨论】:

  • 感谢您的帮助,我会在星期一尝试,但我有一个小问题。我创建的每个测试用例都是一个不同的 python 文件,我没有将所有测试用例都写在一个文件中。您示例中的结构在我的测试套件中是否成功?
  • 如果您有多个带有测试的文件,并且您想在所有测试用例中使用一个且唯一的驱动程序实例,您将需要在conftest.py 文件 - docs.pytest.org/en/3.5.0/… 中定义driver 夹具
猜你喜欢
  • 2019-04-29
  • 1970-01-01
  • 2012-04-03
  • 2020-02-29
  • 2013-02-20
  • 1970-01-01
  • 2016-12-09
  • 2012-09-17
  • 2016-08-16
相关资源
最近更新 更多