[英]How to repeat running tests in python selenium using unittest?
下面顯示了我的測試課程。 當我一次調用它python3 main.py
時,這是一個很好的測試。 如果我希望此測試運行100次,則會出現問題。 怎么做? 當我嘗試通過pytest打電話時,有這樣的警告:
main.py::TestLoginPage::test_login_invalid_credentials
/home/-----/.local/lib/python3.5/site-packages/pytest_repeat.py:31: UserWarning: Repeating unittest class tests not supported
"Repeating unittest class tests not supported")
main.py::TestLoginPage::test_login_valid_credentials
/home/-----/.local/lib/python3.5/site-packages/pytest_repeat.py:31: UserWarning: Repeating unittest class tests not supported
"Repeating unittest class tests not supported")
這是我的測試課程-test_login_page.py
import unittest
from selenium import webdriver
from tests.test_driver import TestDriver
from pages.login_page import LoginPage
class TestLoginPage(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.page_url = LoginPage.login_url
cls.webdriver = webdriver
TestDriver.setUp(cls, cls.page_url)
cls.page = LoginPage(cls.webdriver)
cls.option = 1
def __init_test_method(self):
# [TS001].Check if page is loaded correctly
self.assertTrue(self.page.check_page_loaded_correctly)
# [TS002].Check button contains 'login' text
self.assertEqual(self.page.get_button_text(), 'Login')
# TC001
def test_login_valid_credentials(self):
"""Test login with valid credentials"""
self.__init_test_method()
# [TS003].Clear form, fill with correct data and submit
self.assertEqual(self.page.login_process(self.option), 'Complexes')
# TC002 (invalid password) & TC003 (invalid username)
def test_login_invalid_credentials(self):
"""Test login with invalid credentials"""
for option in range(2, 4):
self.__init_test_method()
self.assertTrue(self.page.login_process(option))
@classmethod
def tearDownClass(cls):
cls.webdriver.quit()
這是主要的,我從控制台運行所有測試-main.py
import unittest
import os
import sys
cwd = os.getcwd()
sys.path.append(cwd)
from tests.test_login_page import TestLoginPage
if __name__ == '__main__':
unittest.main()
如果要反復運行測試,建議您使用ddt或數據驅動的測試。 您可以在此處找到完整使用的文檔。
您將使用ddt裝飾測試類,並使用file_data或data裝飾測試方法以傳遞唯一的測試參數。
這樣,您就可以為每次測試的迭代提供新的測試參數,並且可以循環多次並運行相同的測試方法,無論您需要多少次。
這是我如何使用它的一個例子:
from ddt import ddt, file_data
@ddt
class MyTestClass(unittest.TestCase):
@file_data('test_parameter_file.json')
def some_test_method1(self, test_package):
Some test method
我在test_parameter_file.json文件中具有不同的測試參數,並且我的測試方法針對文件中的每個參數運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.