Search code examples
pytestpython-poetry

Pytest does not find my tests in Poetry project (VSCode finds)


I've just created my first Python package using Poetry using the usual poetry new mypackage command. My problem is that pytest does not execute any test when I run it. I'm developing using VSCode and the weird behavior is that VSCode successfully finds and executes my tests.

Poetry created a subdir called mypackage and another called tests. My test file is called tests/test_mypackage.py.

VSCode autodiscoverd the tests, and display them in the test tab. The .vscode/settings.json file has this configuration:

"python.testing.pytestArgs": [
        "tests"
    ],

I've tried the following commands to execute pytest:

  1. With my venv manually activated:
    1. pytest
    2. pytest tests
    3. pytest tests/test_mypackage.py
    4. cd tests;pytest test_mypackage.py
  2. without my venv activated:
    1. poetry run pytest
    2. poetry run pytest tests

The behavior is always the same: nothing happens, as if pytest couldn't detect anything to run.

I've been using VSCode to run the tests, but now I want to put the code under Continuous Integration. How do I run pytest to validate my package?

UPDATE: from inside the virtual env pytest does not prints any output when run, but its return code is 1.


Solution

  • pytest still does not work when running within the activated virtual environment, but I discovered that I can execute it running:

    poetry run pytest 
    

    I still do not understand why it can't find the test when directly run from the command line even with the venv activated.