Pytest stuck on 'collecting…'

Question:

I have a file new_script_piped.py with a function I want to test:

def columns_preparation(df):

    df.COLUMN_1.replace({'-999':'don't consider'}, inplace=True)
    ...more lines

    return df

and the test file test_new_script_piped.py

import pandas as pd
import new_script_piped as x

def test_columns_preparation(df):
    
    test_df = pd.read_excel('test_column_preparation.xlsx')
    result_df = pd.read_excel('test_column_preparation_result.xlsx')

    assert x.columns_preparation(test_df) == result_df

All files are in the same directory. I have tried on the command line:

python3 -m pytest new_script_piped.py
pytest new_script_piped.py
python -m pytest new_script_piped.py

and they all get suck at:

platform darwin -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: /Users/31275553/Documents/
collecting ... 
Asked By: Pab

||

Answers:

You have two options:

  • Create a separate module with these two files and run pytest from this directory

  • Specify file/folder to look for tests by executing pytest test_new_script_piped.py

Since pytest recursively looking for tests the issue could be caused by executing pytest command inside the directory with a lot of files (which could be true, judging by rootdir: /Users/31275553/Documents/ provided by you in the snippet).

If none of the options would work – it would be great to know what is inside the /Users/31275553/Documents/ directory

Answered By: finomayato

I was able to reduce the “Collecting…” delay by a significant margin by narrowing down which directories are searched for tests, which achieves the same effect as finomayato’s answer, through config rather than supplying the path to the tests as an argument.

Based on example from the docs, let’s say you want to make sure pytest isn’t digging down into your git, build, or frontend dependency directories looking for tests:

# content of pytest.ini
[pytest]
norecursedirs = .git build node_modules

https://docs.pytest.org/en/latest/example/pythoncollection.html#changing-directory-recursion

Answered By: JHS

Your test is probably taking a long time

pytest -s path_to_script.py  

In order to see stdout if you have any progress prints

Answered By: Daniel Braun

I uninstalled pytest-dbfixtures in pip list then module notfound error resolved

Answered By: bhargav

In case someone is still facing this issue and none of their code can lead to infinite loop, try deleting pycache directories that could help.

Answered By: Chandan Singh

In my case it was completely a different story. I was using local postgresql server and consuming it for the test db and after spending a whole day, I got to know that my db connection pool size is already reached to the limit hence test db connection was not successful. After that I deleted the system processes(async tests, I ran previously) consuming the db connections in the background. After killing the first process my test case automatically ran. Hope it helps someone.

Answered By: vikas0713
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.