Я использую pytest-django для тестирования моего приложения django. Моя структура каталогов выглядит следующим образом:
.
├── Dockerfile
├── Pipfile
├── Pipfile.lock
├── README.md
├── app
│ ├── __pycache__
│ │ └── test_example.cpython-37-pytest-5.2.1.pyc
│ ├── app
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-37.pyc
│ │ │ ├── settings.cpython-37.pyc
│ │ │ └── urls.cpython-37.pyc
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ ├── core
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-37.pyc
│ │ │ ├── admin.cpython-37.pyc
│ │ │ └── models.cpython-37.pyc
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── migrations
│ │ │ ├── 0001_initial.py
│ │ │ ├── __init__.py
│ │ │ └── __pycache__
│ │ │ ├── 0001_initial.cpython-37.pyc
│ │ │ └── __init__.cpython-37.pyc
│ │ ├── models.py
│ │ └── tests
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-37.pyc
│ │ │ ├── test_admin.cpython-37-pytest-5.2.1.pyc
│ │ │ └── test_models.cpython-37-pytest-5.2.1.pyc
│ │ ├── test_admin.py
│ │ └── test_models.py
│ ├── db.sqlite3
│ ├── manage.py
│ ├── pytest.ini
│ └── test_example.py
└── docker-compose.yml
Файл: test_models.py выглядит следующим образом:
from django.contrib.auth import get_user_model
import pytest
@pytest.mark.django_db
class TestUserCreation:
def test_create_user_with_email_successfull(self):
"""
Test creating a new user with an email is successfull
"""
email = "test@gmail.com"
password = "testpass123"
user = get_user_model().objects.create_user(
email=email,
password=password
)
assert user.email == email
assert user.check_password(password)
def test_new_user_email_normalized(self):
"""Test the email for a new user is normalized"""
email = "test@GMAIL.COM"
user = get_user_model().objects.create_user(
email=email,
password="Testpass123"
)
assert user.email == email.lower()
def test_new_user_invalid_email(self):
"""Test creating user with no email raises error"""
with pytest.raises(ValueError):
get_user_model().objects.create_user(
email=None,
password="testpass123"
)
def test_create_new_super_user(self):
"""Test creating a super user"""
user = get_user_model().objects.create_superuser(
email='test@gmail.com',
password='testpass123',
)
assert user.is_superuser
assert user.is_staff
, а файл: test_admin.py выглядит так:
import pytest
from django.contrib.auth import get_user_model
from django.urls import reverse
def test_something_simple():
names = ["Subhayan", "Shaayan"]
assert len(names) == 2
когда я запускаю pytest из корня моего проекта:
(recipe-app-api) ~/Desktop/Studies/Codes/recipe_rest_api/recipe-app-api:master$ pytest -v
============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- /Users/subhayanbhattacharya/.local/share/virtualenvs/recipe-app-api-xtbpUqq8/bin/python3.7
cachedir: .pytest_cache
rootdir: /Users/subhayanbhattacharya/Desktop/Studies/Codes/recipe_rest_api/recipe-app-api
plugins: django-3.5.1
collected 6 items
app/core/tests/test_models.py::TestUserCreation::test_create_user_with_email_successfull SKIPPED [ 16%]
app/core/tests/test_models.py::TestUserCreation::test_new_user_email_normalized SKIPPED [ 33%]
app/core/tests/test_models.py::TestUserCreation::test_new_user_invalid_email SKIPPED [ 50%]
app/core/tests/test_models.py::TestUserCreation::test_create_new_super_user SKIPPED [ 66%]
app/test_example.py::test_example PASSED [ 83%]
app/core/tests/test_admin.py::test_something_simple PASSED [100%]
========================================================================== 2 passed, 4 skipped in 0.42s ==========================================================================
Но очень странно, когда я захожу в папку приложения:
(recipe-app-api) ~/Desktop/Studies/Codes/recipe_rest_api/recipe-app-api:master$ cd app
(recipe-app-api) ~/Desktop/Studies/Codes/recipe_rest_api/recipe-app-api/app:master$ pytest -v
============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- /Users/subhayanbhattacharya/.local/share/virtualenvs/recipe-app-api-xtbpUqq8/bin/python3.7
cachedir: .pytest_cache
Django settings: app.settings (from ini file)
rootdir: /Users/subhayanbhattacharya/Desktop/Studies/Codes/recipe_rest_api/recipe-app-api/app, inifile: pytest.ini
plugins: django-3.5.1
collected 6 items
core/tests/test_models.py::TestUserCreation::test_create_user_with_email_successfull PASSED [ 16%]
core/tests/test_models.py::TestUserCreation::test_new_user_email_normalized PASSED [ 33%]
core/tests/test_models.py::TestUserCreation::test_new_user_invalid_email PASSED [ 50%]
core/tests/test_models.py::TestUserCreation::test_create_new_super_user PASSED [ 66%]
test_example.py::test_example PASSED [ 83%]
core/tests/test_admin.py::test_something_simple PASSED [100%]
=============================================================================== 6 passed in 0.81s ================================================================================
Я не уверен, чтонеправильно . Может кто-нибудь, пожалуйста, пролить немного света на это. Файл pytest.ini выглядит следующим образом
DJANGO_SETTINGS_MODULE = app.settings(recipe-app-api)