【发布时间】: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