Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 264
Ramkumar Hariharan Avatar asked May 20 '16 18:05

Ramkumar Hariharan


People also ask

How do I stop pytest execution?

pytest has the option -x or --exitfirst which stops the execution of the tests instanly on first error or failed test. pytest also has the option --maxfail=num in which num indicates the number of errors or failures required to stop the execution of the tests.

How do I skip Testcases in pytest?

The simplest way to skip a test is to mark it with the skip decorator which may be passed an optional reason . It is also possible to skip imperatively during test execution or setup by calling the pytest. skip(reason) function. This is useful when it is not possible to evaluate the skip condition during import time.

What does S mean in pytest output?

S means skipped, . means success.


2 Answers

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.

like image 193
cewing Avatar answered Sep 19 '22 12:09

cewing


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.

like image 24
Ivan Camilito Ramirez Verdes Avatar answered Sep 17 '22 12:09

Ivan Camilito Ramirez Verdes