pytest using Makefile and make command

Question:

I am learning Test Driven development for the first time. I have no experience of software development, but have some experience with scripting.

I have been following LinuxAcademy Python 3 for Sys Admin tutorial.

I created the following structure,

├── Makefile
├── Pipfile
├── Pipfile.lock
├── README.rst
├── setup.py
├── src
│   └── pgbackup
│       ├── cli.py
│       └── __init__.py
└── tests
    └── test_cli.py

setup.py file,

from setuptools import setup, find_packages
    with open('README.rst', 'r') as f:
        readme = f.read()
    setup(
        name='pgbackup',
        version='0.1.0',
        description='Database backups locally or to AWS S3.',
        long_description=readme,
        author='Keith Thompson',
        author_email='[email protected]',
        packages=find_packages('src'),
        package_dir={'': 'src'},
    )

Makefile file,

.PHONY: install test

default: test

install:
        pipenv install --dev --skip-lock

test:
        PYTHONPATH=./src pytest

tests/test_cli.py file,

import pytest
from pgbackup import cli

def test_helloworld():
    """
    JUST A HELLO WORLD TEST
    """
    assert cli.hello() == "helloworld"

and src/pgbackup/cli.py file,

def hello():
    return "helloworld"

I wrote helloworld as my first sample test it is not the part of the tutorial. Now when I run make command from project root directory, my test is passed,

========================================== test session starts ===========================================platform linux -- Python 3.6.6, pytest-3.8.0, py-1.6.0, pluggy-0.7.1
rootdir: /root/code/pgbackup, inifile:
collected 1 item

tests/test_cli.py .                                                                                [100%]

======================================== 1 passed in 0.04 seconds ========================================

I know the make command is setting PYTHONPATH to ./src pytest but not get my head around how its running actual test? I know its only setting a search path to import python modules.

If I try to run pytest command from tests dir, my test if failed,

================================================= ERRORS =================================================___________________________________ ERROR collecting tests/test_cli.py ___________________________________ImportError while importing test module '/root/code/pgbackup/tests/test_cli.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test_cli.py:2: in <module>
    from pgbackup import cli
E   ModuleNotFoundError: No module named 'pgbackup'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!======================================== 1 error in 0.35 seconds =========================================

If i run test from src dir, it doesn’t run anything,

====================================== no tests ran in 0.01 seconds ======================================

Can someone please explain how running make runs the test since Makefile is just setting PYTHONPATH variable?

Asked By: MaverickD

||

Answers:

Can someone please explain how running make runs the test since Makefile is just setting PYTHONPATH variable?

It’s not only setting variable. pytest is being run here because your current line for test:

PYTHONPATH=./src pytest

is equivalent to:

export PYTHONPATH=./src; pytest

Check 3rd example here: Command not found error in Bash variable assignment for explanation

You will probably want to change it to look accordingly,

If I try to run pytest command from tests dir, my test if failed,

As for running pytest from different directories. Indeed, this will give different results. pytest searches for tests (looks for all files in test folder, or current one named test_*). So if you run it inside src, it won’t find any tests.

If I try to run pytest command from tests dir, my test if failed

pgbackup seem to be imported from current directory so when you move inside ‘tests’ folder it won’t be found.

Hope this helps.

Answered By: Eduard
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.