0

Below pytest code works fine, which increments value.

import pytest
pytest.value = 1

def test_1():
    pytest.value +=1
    print(pytest.value)

def test_2():
    pytest.value +=1
    print(pytest.value)

def test_3():
    pytest.value +=1
    print(pytest.value)

Output:

Prints
2
3
4

I don't want to execute test_2, when value=2

Is it possible by pytest.dependency() ? If yes, how can i use variable value in pytest.dependency ?

If not pytest.dependency, Any alternative ?

or any better way of handling such scenarios ?

    import pytest
    pytest.value = 1
    
    def test_1():
        pytest.value +=1
        print(pytest.value)
    
    @pytest.dependency(value=2)  # or @pytest.dependency(pytest.value=2)
    def test_2():
        pytest.value +=1
        print(pytest.value)
    
    def test_3():
        pytest.value +=1
        print(pytest.value)

Can you please guide me ? Can this be done ? Is this possible ?

StackGuru
  • 471
  • 1
  • 9
  • 25
  • Is this achievable ? Can anyone guide here ? – StackGuru Jul 15 '20 at 18:38
  • I haven't used `pytest.dependency`, but from what I see in the documentation, there is no such option - at least I couln't find it. What is it that you actually want to achieve? – MrBean Bremen Jul 15 '20 at 18:56
  • @MrBean: Thanks for responding. Scenario is to skip rest all test cases whenever value of 'value' =2. This 'value' keeps changing dynamically, based on test actions. – StackGuru Jul 15 '20 at 20:55
  • @MrBeanBremen : Could think of anything ? – StackGuru Jul 16 '20 at 06:55
  • 1
    Does this answer your question? [Pytest skip test with certain parameter value](https://stackoverflow.com/questions/45676648/pytest-skip-test-with-certain-parameter-value) – MrBean Bremen Jul 16 '20 at 07:08
  • @MrBeanBremen Thanks for that, had gone through that already. Actually, if there are many testcases, adding conditions like this in every test-case not a good idea. So, i thought to handle such stuffs outside test-case, i.e., pytest.dependency or pytest.skip – StackGuru Jul 16 '20 at 07:14

1 Answers1

1

If you have access to the value outside of the test (as it is the case in your example), you can skip the tests in a fixture based on the value:

@pytest.fixture(autouse=True)
def skip_unwanted_values():
    if pytest.value == 2:
        pytest.skip(f"Value {pytest.value} shall not be tested")

In the example given above, where pytest.value is set to 2 after test_1, test_2 and test_3 would be skipped. Here is the output I get:

...
test_skip_tests.py::test_1 PASSED                                        [ 33%]2

test_skip_tests.py::test_2 SKIPPED                                       [ 66%]
Skipped: Value 2 shall not be tested

test_skip_tests.py::test_3 SKIPPED                                       [100%]
Skipped: Value 2 shall not be tested
failed: 0


======================== 1 passed, 2 skipped in 0.06s =========================
MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46
  • 'value' is not static. it's dynamic. If you look at snippet posted above, Initially, when ''value" is 1 , it executes test-cases and alters the 'value' based on some condition. When the 'value' changes to 2, should skip rest all the test cases. – StackGuru Jul 16 '20 at 07:49