0

This is my conftest.py:

class ValueStorage:
    ime_rute = None
    web_driver = None


@pytest.fixture(params=["chrome"], scope="class")
def init_driver(request):
    start = datetime.now()
    tester = os.environ["USERNAME"]
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)
    logger.addHandler(logging.FileHandler(filename=f'.\\Reports\\{date.today()}_duration.log'))
    if request.param == "chrome":
        options = ChromeOptions()
        options.add_argument("--start-maximized")
        options.add_argument("--no-sandbox")
        if Options.HEADLESS == 1:
            options.add_argument('--headless')
        options.add_experimental_option("excludeSwitches", ["enable-logging"])
        ValueStorage.web_driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
    if request.param == "firefox":
        ValueStorage.web_driver = webdriver.Firefox(GeckoDriverManager().install())
    request.cls.driver = ValueStorage.web_driver
    yield
    end = datetime.now()
    logger.info(
        f"{end}: --- DURATION: {end - start} --- ROUTE NAME: {ValueStorage.ime_rute} --- HEADLESS: {Options.HEADLESS} --- RUN BY: {tester}")
    ValueStorage.web_driver.close()

I'm running the tests from the command line using

pytest -k example -vv

(where "example" is the test name).

I wish to repeat the test multiple times, so I'm using @pytest.mark.repeat(100) decorator:

    @pytest.mark.repeat(100)
    @pytest.mark.develop
    def test_example(self):
        # test code

Output in the duration.log file will be like

2021-04-29 12:10:57.245345: --- DURATION: 0:00:24.612506 --- ROUTE NAME: 210429-3843-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:11:21.726882: --- DURATION: 0:00:24.364809 --- ROUTE NAME: 210429-3844-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:11:21.726882: --- DURATION: 0:00:24.364809 --- ROUTE NAME: 210429-3844-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:11:57.758704: --- DURATION: 0:00:35.897821 --- ROUTE NAME: 210429-3845-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:11:57.758704: --- DURATION: 0:00:35.897821 --- ROUTE NAME: 210429-3845-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:11:57.758704: --- DURATION: 0:00:35.897821 --- ROUTE NAME: 210429-3845-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:12:28.073752: --- DURATION: 0:00:30.255046 --- ROUTE NAME: 210429-3846-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:12:28.073752: --- DURATION: 0:00:30.255046 --- ROUTE NAME: 210429-3846-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:12:28.073752: --- DURATION: 0:00:30.255046 --- ROUTE NAME: 210429-3846-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1
2021-04-29 12:12:28.073752: --- DURATION: 0:00:30.255046 --- ROUTE NAME: 210429-3846-RT-JDoe --- HEADLESS: 1 --- RUN BY: tester1

So, what is happening is that the logger.info() line writes to log once for the first test run, twice for the second one, three times for the third and so on.

I tried changing scope of the pytest fixture to 'class', but then I'm getting that line logged only after the test has been run 100 times.

I want to get the logger.info() write to log after each test execution once. What do I need to do?

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
  • You add a new logging handler each time your `init_driver` is executed, so for each new test class, each log will be made by one more handler. You need to add a handler only once (for example, in a session scoped fixture). – MrBean Bremen Apr 29 '21 at 10:31
  • I cannot use session scope. I get "AttributeError: cls not available in session-scoped context". Is there another way? – Mate Mrše Apr 29 '21 at 10:37
  • I meant for the logger, but you also can check if the handler is already added using `logging.Logger.hasHandlers` (see the linked answer above). – MrBean Bremen Apr 29 '21 at 10:39
  • Thanks, the linked question was helpful. – Mate Mrše Apr 29 '21 at 14:48

0 Answers0