简体   繁体   中英

How to repeat running tests in python selenium using unittest?

Below is displayed my test class . This is good test when i call it once - python3 main.py . There is a problem if I want this test run for example 100 times. How to do that? When I try call by pytest there are warnings sth like this:

    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")

This is my test class - 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()

This is main which I run all tests from console - 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()

If you want to run your tests over and over, I would suggest you use ddt or data driven tests. You can find the documentation for complete usage here .

You will decorate your test class with ddt and your test method with file_data or data to pass in unique test parameters.

This will allow you to feed new test parameters for each iteration of your test and will cycle through and run the same test method for however many times you need.

Here is an example of how I use it:

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

I have the different test parameters in the test_parameter_file.json file and my test method is run for each parameter in the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM