mungayree
mungayree

Reputation: 309

pytest -3.2.2 duplicate test

test_set.py contains

 def test_a():
     pass

 def test_b():
     pass

 def test_a():
     pass 

 def test_a():
     pass

running pytest --keep-duplicates test.py run only one instance of test_a to show

 test::test_a   PASSED
 test::test_b   PASSED 

how Can I run test.py to run test_a (thrice) ?

Upvotes: 0

Views: 1408

Answers (1)

Francesco Montesano
Francesco Montesano

Reputation: 8668

As explained in the documentation, the --keep-duplicates is useful to run tests if you have files with the same name in different directories.

The short answer is that it is not possible to do what you want.


A longer answer is that when a variable is defined multiple times in a file, the last definition overwrite all the previous ones. This is true for simple variable assigmen as well as for anything else.

For example, if you have a file hitchhiker.py with the following code:

a_variable = 42
[some code]
a_variable = 'towel'

and you import it, a_variable will have the value 'towel'.

If you modify your test file in the following file:

def test_a():
    print('a1')

def test_b():
    print('b')

def test_a():
    print('a2')

def test_a():
    print('a3')

and then run it, you get:

-> py.test test_a.py -s -v
[...]
collected 2 items 

test_a.py::test_b b
PASSED
test_a.py::test_a a3
PASSED

As you can see the function last defined in the file is executed. If you move the first function at the end of the file, you will see that a1 is printed.

Upvotes: 2

Related Questions