Python Selenium 运行 多重测试 类 在一个 selenium webdriver 中使用 pytest
Python Selenium run multiple test classes in one selenium webdriver using with pytest
我是 python 的新手,我开始创建一个关于 GUI 的自动化测试套件(不同文件中的多个测试用例)。我想在一个 selenium webdriver 中执行我所有的测试用例,所以我创建了一个单例 webdriver class 并且我想使用这个 class 我所有的测试用例。这是我的单身网络驱动程序 class:
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 与传统 XUnit 系列测试运行器相比最大的特点和优势是它有固定装置。我邀请你使用它。在您的场景中,我将摆脱扩展 unittest.TestCase
和 setUp
方法,转而使用 pytest fixture,如下所示:
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 的新手,我开始创建一个关于 GUI 的自动化测试套件(不同文件中的多个测试用例)。我想在一个 selenium webdriver 中执行我所有的测试用例,所以我创建了一个单例 webdriver class 并且我想使用这个 class 我所有的测试用例。这是我的单身网络驱动程序 class:
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 与传统 XUnit 系列测试运行器相比最大的特点和优势是它有固定装置。我邀请你使用它。在您的场景中,我将摆脱扩展 unittest.TestCase
和 setUp
方法,转而使用 pytest fixture,如下所示:
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')