0

I have a code which includes: test class, tests, fixtures, parameterizing Like this:

import pytest

@pytest.fixture
def num():
     return 1


@pytest.mark.parameterize('n', [1, 2])
class TestNum:

     def test_num(self, num, n):
         if n == 2:
             pytest.skip()
         assert num == n

But I want something like this:

import pytest

@pytest.fixture
def num():
     return 1


@pytest.mark.parameterize('n', [1, 2])
class TestNum:
    
     @pytest.mark.skipif(n == 2, reason='no reason to test that')
     def test_num(self, num, n):
         assert num == n

Question: is it possible to skip test depending the class parameter value from "@pytest.mark.parametrize('n', [1, 2])", before fixture run?

Why "if [condition]: pytest.skip()" does not satisfy me:

I work on the web app project, using Playwright framework and my code is like this:

import pytest
from playwright.sync_api import Page

@pytest.fixture
def new_page(page: Page):
     page.goto(URL)
     return page


@pytest.mark.parameterize('n', [1, 2])
class TestA:

     def test_a(self, n, new_page):
         if n == 2:
             pytest.skip()
         assert True

There are fixtures that create a web_page(page) when test starts, and I have a few such fixtures in my test, which create several pages. So the main issue is to skip test depending the parameter in mark.parametrize before fixtures run, for time saving

After long searching, and theory review, there is no answer to my specific question

mknarespe
  • 1
  • 1
  • I don't think what you're describing is possible. What I would recommend is creating another fixture that is itself parametrized (instead of the class) and guaranteed to run first. – Teejay Bruno Feb 18 '23 at 00:41

0 Answers0