1

I am trying to run a pytest method multiple times using pytest-repeat but i am getting a warning and its running only once

from page.to_run_login import RegisterLogin
from utilites.testStatus import TestStatus
import unittest
import pytest



@pytest.mark.usefixture("oneTimeSetUp","setUp")
class RegisterTest(unittest.TestCase):

@pytest.fixture(autouse=True)
def classSetup(self,oneTimeSetUp):
    self.rg = RegisterLogin(self.driver)
    self.ts = TestStatus(self.driver)

@pytest.mark.run(order=1)
def test_registerLink(self):
    self.rg.register()
    self.rg.select_state_name()
    self.rg.select_city_name()
    self.rg.select_ready_wait()
    self.rg.select_ready_pay()
    self.rg.select_submit()

In terminal i am executing using this command py.test -s -v test/to_test_login.py --count 2

manoj
  • 121
  • 3
  • 14
  • 1
    From the readme: _Unfortunately pytest-repeat is not able to work with unittest.TestCase test classes. These tests will simply always run once, regardless of --count, and show a warning._ – hoefling Jun 05 '18 at 07:06
  • is there any other way where i can run it multiple times?? – manoj Jun 05 '18 at 07:12
  • For unittest see https://stackoverflow.com/a/63712502/9201239 – stason Sep 02 '20 at 19:41

3 Answers3

3

As of Oct 31, 2020 there is a better way to do this. See https://pypi.org/project/pytest-repeat/


Simply pip install pytest-repeat and use --count. Example: pytest --count=10 test_file.py

Jortega
  • 3,616
  • 1
  • 18
  • 21
1

If you're using pytest with a test that inherits unittest.TestCase, there's a simple way of repeating it by using the parameterized Python library:

from unittest import TestCase
from parameterized import parameterized

class RepeatTests(TestCase):
    @parameterized.expand([[]] * 5)
    def test_repeat_this_test_with_parameterized(self):
        print("Hello!")
        self.assertTrue(True)

Output:

Hello!
Hello!
Hello!
Hello!
Hello!
Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
-1
 @pytest.mark.run(order=1)
 def test_registerLink(self):
    for i in range(100):
        self.rg.register()
        self.rg.select_state_name()
        self.rg.select_city_name()
        self.rg.select_ready_wait()
        self.rg.select_ready_pay()
        self.rg.select_submit()
        self.rg.driver.back()
        self.rg.driver.refresh()

I was able to solve it by doing this

manoj
  • 121
  • 3
  • 14