'Is it possible to run pytest across multiple projects with one command

I have a project involving a number of AWS lambda functions where I want to run pytest at any level to execute the tests within the current directory and its subdirectories.

The structure is

/src
  /lambda-one
    lambda_handler.py
    __init__.py
    /test
       test_lambda_handler.py
      __init__.py
  /group-a
    /lambda-two
      lambda_handler.py
      __init__.py
      /test
         test_lambda_handler.py
         __init__.py
    /lambda-three
      lambda_handler.py
      __init__.py
      /test
         test_lambda_handler.py
         __init__.py
  /group-b
    /lambda-four
      lambda_handler.py
      __init__.py
      /test
         test_lambda_handler.py
         __init__.py
    /lambda-five
      lambda_handler.py
      __init__.py
      /test
         test_lambda_handler.py
         __init__.py

To run the tests I either have to change directory to the actual lambda or change my import paths to absolute paths from src.

Is there a way to have pytest run at each lambda level (without a shell script) so I can get a single set of results.



Solution 1:[1]

Yes. You can configure it on pytest.ini. Here is some example.

I have this directory structure.

.
??? pytest.ini
??? src
    ??? __init__.py
    ??? lambda_one
    ?   ??? __init__.py
    ?   ??? lambda_handler.py
    ?   ??? test
    ?       ??? __init__.py
    ?       ??? test_lambda_handler.py
    ??? lambda_two
        ??? __init__.py
        ??? lambda_handler.py
        ??? test
            ??? __init__.py
            ??? test_lambda_handler.py

Inside pytest.ini, I put this configuration.


[pytest]
python_files = */test/*.py

And here is the result of running pytest command outside the src/ directory.

$ pytest
============================================================== test session starts ===============================================================
platform linux -- Python 3.8.10, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /home/imad/TheHumbleProgrammer/stackoverflow/pytest-multiple-projects, configfile: pytest.ini
plugins: dash-1.20.0
collected 2 items                                                                                                                                

src/lambda_one/test/test_lambda_handler.py .                                                                                               [ 50%]
src/lambda_two/test/test_lambda_handler.py .                                                                                               [100%]

=============================================================== 2 passed in 0.02s ================================================================

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Imaduddin A Majid