pythonpython-3.xunit-testingpytest

PyTest: "no tests ran" even though tests were clearly run


I have the following config file for pytest:

[pytest] 
filterwarnings = 
    ignore::DeprecationWarning
    ignore::UserWarning 

python_files = 
    test_file_1.py 
    test_file_2.py

log_cli = True 
log_level = INFO 

I run pytest locally with the following command:

python -m pytest path

and get the output

====================================================================================================== no tests ran in 24.77s =======================================================================================================

which I cannot believe, since before, I get the following output in the terminal (because of the logging):

INFO     root:test_file_1.py:40 torch.Size([100, 71])

I noticed that if I remove the filterwarnings in pytest.ini, I get a warnings summary, but not the output "no tests ran".

What exactly is happening here? Why does filterwarnings lead to the output "no tests ran"?


Solution

  • pytest expects test methods to be named a certain way. Eg def test_1():.

    If you just throw python code into a file and run it with pytest:

    import time
    
    time.sleep(2)
    

    You'll see: no tests ran in 2.01s.

    But if you have a method that starts with test_ in your file and run it with pytest:

    import time
    
    def test_1():
        time.sleep(2)
    

    You'll see: 1 passed in 2.01s.

    So it's important to follow the naming conventions set by pytest if you want your tests to be discovered. (More info on discovery rules: https://docs.pytest.org/en/latest/example/pythoncollection.html)

    Have your files start with test_, and have your methods start with test_ so that the default pytest discovery rules find your tests.