我刚刚将 pytest 添加到现有的 Django 项目中 - 所有单元测试都使用 Django 的unittest子类等。我们使用 SQLite 内存数据库进行测试。
manage.py test我们的测试套件大约需要 80 秒py.test采取相同的py.test -n1(或-n4,或类似的东西)大约需要 1280 秒。我期望有开销来支持该分发,但显然-n4,在大型测试套件上,它应该快大约 3-4 倍。
到目前为止,我已将问题追溯到数据库访问。测试运行得很快,直到第一次访问数据库,但在第一次.save()调用 Django 模型时,该测试将非常慢。
对工人进行一些分析后,看起来他们花了很多时间等待锁,但我不知道这是否是一个可靠的发现。
我想知道数据库上是否存在某种锁定,有人建议我内存中的 SQLite 数据库可能是一个内存映射文件,并且锁定可能发生在工作人员之间,但显然每次调用 open使用 SQLite 的内存数据库将返回一个完全独立的实例。
就目前情况而言,到目前为止,我可能已经在这方面花费了 5 个多小时,并与同事和其他人详细讨论了这一问题,但尚未发现问题。我无法在单独的代码库上重现。
预先感谢您的任何想法!
我有一个index.html,其中包含一个nav.html,其中有一个指向不存在的路由名称的url 标记。当我运行索引视图测试时,响应代码为 200 并且测试通过。如果我manage.py runserver导航到浏览器中的索引,我会收到 NoReverseMatch 错误消息页面。当我从index.html 中删除include 并将nav.html 的内容直接放入index.html 时,测试按预期失败。
如何编写一个测试来捕获包含的模板中的问题?
导航.html
{% url 'project:wrong_name' %}
Run Code Online (Sandbox Code Playgroud)
索引.html
{% include 'project/nav.html' %}
Run Code Online (Sandbox Code Playgroud)
视图.py
def index(request):
return render(request, 'project/index.html')
Run Code Online (Sandbox Code Playgroud)
测试.py
def test_index_view(client):
response = client.get(reverse('project:index')
assert response.status_code == 200
Run Code Online (Sandbox Code Playgroud)
设置.py
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
...
Run Code Online (Sandbox Code Playgroud)
virtualenv(缩写):
python==3.6.1
Django==1.11
pytest-django==3.1.2
Run Code Online (Sandbox Code Playgroud) 如何使用pytest.ini,tox.ini或默认setup.cfg运行的位置设置环境变量pytest?
我创建了一个 docker 容器,其卷指向我的项目目录,所以我所做的每一个更改也在 docker 容器内可见。问题是pytest.ini我的项目根目录中有一个不适用于 docker 容器的文件。
所以我想在 docker 容器内设置一个环境变量来指定在哪里查找 pytest 配置。有谁知道我该怎么做?
我正在使用 pytest 进行自动化。
pytest 中是否有任何选项可以在另一个测试用例中调用测试用例。
我正在使用 Django 1.11.9 和django-pytest库来测试我的应用程序。另外,我使用 Redis 作为缓存存储。我的问题是 \xe2\x80\x94 如何制作测试缓存存储并在运行测试之前使用测试数据设置它?与 Django 数据库的方式类似。
我想添加一些key: value数据来测试缓存存储(在 Redis 中),运行测试,然后删除所有这些测试数据(清除测试缓存)。
我需要在使用夹具后显式删除它。我知道 pytest-django 默认情况下会在拆卸时删除所有对象,但在这种特殊情况下,我需要手动执行此操作。然而,虽然我的测试被标记为pytest.mark.django_db,但我能够创建一个固定装置,但无法在一行后删除它yield:
import pytest
from tgapps.models import TelegramApp
@pytest.fixture(scope='module')
def some_fixture():
app = TelegramApp.objects.create(
session_data=b'\xa2\x8f#',
app_owner_phone=79856235474,
app_id=182475,
app_hash='aad9ab4384fea1af0342b77b606d13b0'
)
yield app
print('deleting object...')
app.delete()
class TestTelegramServiceObject(object):
@pytest.mark.django_db
def test1(self, some_fixture):
print('Fixture created:')
print(some_fixture)
Run Code Online (Sandbox Code Playgroud)
这是我的测试输出:
============================= test session starts ==============================
platform darwin -- Python 3.6.4, pytest-3.4.0, py-1.5.2, pluggy-0.6.0
Django settings: inviter.settings.staging (from ini file)
rootdir: /Users/1111/_projects/fasttrack/inviter, inifile: pytest.ini
plugins: mock-1.7.1, dotenv-0.1.0, django-3.1.2
collected 1 item
test_example.py E.Fixture created:
<79856235474 - 182475>
deleting object...
tests/api/test_example.py:25 (TestTelegramServiceObject.test1)
@pytest.fixture(scope='module') …Run Code Online (Sandbox Code Playgroud) import pytest
from . import utilization
pytestmark = pytest.mark.django_db
some_period = 'test'
@pytest.mark.django_db
class TestUtilization(object):
@classmethod
def setup_class(self):
self.period = some_period
self.metric = utilization.Utilization(period=self.period)
print('setup')
def test_get_query_params(self, tprint):
"""Should return dict with start/end keys of datetime values"""
# assert self.xxx == some_period
tprint(self.period)
tprint(self.metric)
def test_call(self, tprint):
"""Test that we have desired fields and index of the metric"""
pass
Run Code Online (Sandbox Code Playgroud)
我得到这个:
E Failed: Database access not allowed, use the "django_db"
mark, or the "db" or "transactional_db" fixtures to enable it.
Run Code Online (Sandbox Code Playgroud)
我遇到了在运行 pytest 之前从未见过的奇怪错误,我正在运行测试,并且几乎所有错误都由于不允许访问数据库而出错。
这是上周没有发生的新错误,因此它不是本地代码更改,但我不确定到底发生了什么。
有人能指出我正确的方向吗?
这是错误:
conftest.py:52:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/local/lib/python3.8/site-packages/django/db/models/query.py:287: in __iter__
self._fetch_all()
/usr/local/lib/python3.8/site-packages/cacheops/query.py:271: in _fetch_all
return self._no_monkey._fetch_all(self)
/usr/local/lib/python3.8/site-packages/django/db/models/query.py:1308: in _fetch_all
self._result_cache = list(self._iterable_class(self))
/usr/local/lib/python3.8/site-packages/django/db/models/query.py:53: in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
/usr/local/lib/python3.8/site-packages/django/db/models/sql/compiler.py:1154: in execute_sql
cursor = self.connection.cursor()
_ _ _ _ _ …Run Code Online (Sandbox Code Playgroud) 我试图通过在 pytest.ini 文件中包含 testpaths 选项来提高测试收集速度。
在每个应用程序文件夹中,我都有一个tests放置测试的文件,因此我将其包含如下:
[pytest]
testpaths = tests
Run Code Online (Sandbox Code Playgroud)
但是,没有收集任何测试。它返回错误:ERROR: file or directory not found: tests
我缺少什么?
当我运行测试时,我意识到pytest-cov仅显示自动化测试期间触及的文件的覆盖率报告。如何设置它才能显示未触及的文件的覆盖范围?
pytest-django ×10
pytest ×8
django ×5
python ×5
testing ×2
caching ×1
docker ×1
pytest-cov ×1
python-2.7 ×1
python-3.x ×1
redis ×1
unit-testing ×1