'pytest' exits with no error, but with "collected 0 items"

PythonUnit Testing

Python Problem Overview


I have been trying to run unit tests using pytest in Python. I had written a module with one class and some methods inside that class. I wrote a unit test for this module (with a simple assert statement to check equality of lists) where I first instantiate the class with a list. Then I invoke a method on that object (from the class). Both test.py and the script to be tested are in the same folder. When I run pytest on it, I get "collected 0 items".

I am new to pytest, and but I am unable to run their examples successfully. What am I missing here?

I am running Python version 3.5.1 and pytest version 2.8.1 on Windows 7.

My test.py code:

from sort_algos import Sorts

def integer_sort_test():
    myobject1 = Sorts([-100, 10, -10])
    assert myobject1.merge_sort() == [-101, -100, 10]

sort_algos.py is a module containing class Sorts. merge_sort is a method under Sorts.

Python Solutions


Solution 1 - Python

pytest gathers tests according to a naming convention. By default any file that is to contain tests must be named starting with test_, classes that hold tests must be named starting with Test, and any function in a file that should be treated as a test must also start with test_.

If you rename your test file to test_sorts.py and rename the example function you provide above as test_integer_sort, then you will find it is automatically collected and executed.

This test collecting behavior can be changed to suit your desires. Changing it will require learning about configuration in pytest.

Solution 2 - Python

I had the same issue, but my function was called test.py. I never thought that the issue would be the file name.

In the documentation it says:

> pytest will run all files of the form test_*.py or *_test.py in the current directory and its subdirectories. More generally, it follows standard test discovery rules.

Exactly! The name should be test_.py or test_something.py and works for me.

I feel so stupid, hehe.

Solution 3 - Python

I faced this issue too and it took me a whole day to find out that the class name should start with "Test" my class name was "test" instead. That made me run my code successfully.

Make sure your python test file name and method name should also start with "test_"

Solution 4 - Python

This helped me a lot. Initially I named my tests below:

def login():
    print("This is login")

def checkout():
    print("This is checkout")

def logout():
    print("This is logout")

My pytest command ran without errors. However, no results were produced. I also observed:

> collected 0 items

Hence I renamed the above tests to test_login(), test_checkout(), test_logout() and now I got successful results!

Solution 5 - Python

I can't upvote yet but want to chime in with a combo solution that worked for me.

First, per above, I renamed my file with 'test_': test_hello.py.

Second, also per above, I renamed each bit I wanted to test with 'test_'

def test_greeting():
    print('Hello, world!')

def test_checkout():
    print("This is checkout")

def logout():
    print("This is logout")

def test_executable():
    """says hello world by default"""
    out = getoutput({prg})

And finally, to actually run the test, I entered the following on the command line:

python -m pytest -v test_hello.py

Without the -m there is a message the file cannot be opened. It works without the -v, but with it, it breaks down each part being tested. RESULT:

---------------------------------------------
collected 3 items

test_hello.py::test_greeting PASSED                                                                              [ 33%]
test_hello.py::test_checkout PASSED                                                                              [ 66%]
test_hello.py::test_executable FAILED


def test_executable():
        """says hello world by default"""
>       out = getoutput({prg})
E       NameError: name 'getoutput' is not defined

test_hello.py:12: NameError

As you can see, it skipped logout(), the part of the program with no test_, and delivered a useful error message. So, with that simple example, things are running as they should. Thank you to everyone who helped me put it together.

Solution 6 - Python

I ran into this problem as well. And it was not caused by script names, but due to a plugin called "pytest-appium" which I have no intention to use in the first place.

I finally found it out from report: ('c:\python39\lib\site-packages\pytest_appium\plugin.py', 8, 'Skipped: no variables file')

Since plugin.py requires variables which I have no idea what it is for.

@pytest.fixture(scope='session')
def appium(variables):
    if not variables:
        pytest.skip('no variables file')

Solution:

pip uninstall pytest-appium

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRamkumar HariharanView Question on Stackoverflow
Solution 1 - PythoncewingView Answer on Stackoverflow
Solution 2 - PythonIvan Camilito Ramirez VerdesView Answer on Stackoverflow
Solution 3 - PythonShivani GambhirView Answer on Stackoverflow
Solution 4 - PythonRameshbabu LakshmananView Answer on Stackoverflow
Solution 5 - PythonFlora PosteschildView Answer on Stackoverflow
Solution 6 - PythonJueView Answer on Stackoverflow