Pytest

Published: by Creative Commons Licence

Installation

pip install pytest

Pytest in Django

configures pytest.ini

[pytest]
DJANGO_SETTINGS_MODULE = settings.base
python_files = tests.py test_*.py *_tests.py

configures conftest.py


import pytest

@pytest.fixture(scope='session')
def django_db_setup():
    pass

example of test


import pytest

@pytest.mark.django_db
def test_user_create(user_factory):
    user = user_factory()
    assert user.pk == 1

Pytest in Flask

configures pytest.ini

[pytest]
python_files = tests.py test_*.py *_tests.py

example of test


import pytest

@pytest.fixture(scope='session')
def app():
    from app import create_app
    app = create_app()
    return app

@pytest.fixture(scope='session')
def client(app):
    return app.test_client()

def test_index(client):
    response = client.get('/')
    assert response.status_code == 200

Pytest in vscode

Setup pytest in settings.python.testing.pytestArgs

{
    "python.testing.pytestArgs": [
        "tests",
        "-v",
        "-s",
        "--color=yes",
        "--cov=.",
        "--cov-report=html"
    ],
}

More args see pytest