Pytest
Installation
bash
pip install pytest
Pytest in Django
configures pytest.ini
ini
[pytest]
DJANGO_SETTINGS_MODULE = settings.base
python_files = tests.py test_*.py *_tests.py
configures conftest.py
python
import pytest
@pytest.fixture(scope='session')
def django_db_setup():
pass
example of test
python
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
ini
[pytest]
python_files = tests.py test_*.py *_tests.py
example of test
python
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
json
{
"python.testing.pytestArgs": [
"tests",
"-v",
"-s",
"--color=yes",
"--cov=.",
"--cov-report=html"
]
}
More args see pytest