django 为什么pytest不运行我的测试并跳过它们

xsuvu9jc  于 2023-10-21  发布在  Go
关注(0)|答案(1)|浏览(141)

我正在使用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 = "[email protected]"
        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 = "[email protected]"
        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='[email protected]',
            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)
d4so4syb

d4so4syb1#

我有一个类似的问题,当我在pyproject.toml文件中添加以下内容时,它得到了解决:

[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "myproject.settings"

相关问题