3ng7n33r
3ng7n33r

Reputation: 29

pytest only runs first test in file

I'm working on the Code-dojo Tennis Kata and I'm stuck with the silliest problem. My pytest stops after running the first test in the file. My test file looks like this:

from tennis import game
import re

def test_game_start(capsys):
    MaxVsMimi = game()
    MaxVsMimi.result()
    out, err = capsys.readouterr()
    assert bool(re.search(r'Love : Love', out))
    
def p1_scores_once(capsys):
    MaxVsMimi = game()
    MaxVsMimi.score("p1")
    MaxVsMimi.result()
    out, err = capsys.readouterr()
    assert bool(re.match(r'^abc', out))

This is the code:

class game:
    def __init__(self):
        self.scorep1 = "Love"
        self.scorep2 = "Love"
        
    def result(self):
        print(self.scorep1, ":", self.scorep2)

and the output:

:stdout:
============================= test session starts ==============================
platform linux -- Python 3.8.6, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /usr/local/bin/python
cachedir: .pytest_cache
rootdir: /sandbox
collecting ... collected 1 item

test_tennis.py::test_game_start PASSED                                   [100%]

============================== 1 passed in 0.01s ===============================

Why does it not run the second "p1_scores_once" test?

Thanks!

Upvotes: 0

Views: 956

Answers (1)

mousetail
mousetail

Reputation: 8010

Pytest, like many testing libraries including unittest require all test functions to have the word test in the start of the name. You can fix your code simply by changing the name of p1_scores_once:

def test_p1_scores_once(capsys):
    ...

Now the automatic test-finding system will treat the function like a test case.

Upvotes: 3

Related Questions