pytest 相关问题

使用Python进行简单强大的无样板测试。还要为`pytest`标记的问题添加`python`标签。

Pytest 报告导入和常量赋值的覆盖范围中缺少行

我对项目中不同文件的覆盖行为差异感到困惑,希望有人建议如何比我现在更有效地调试它。这基本上是...

回答 1 投票 0

如何在pytest中访问非fixture函数中的fixture

我尝试将测试数据文件路径列表作为命令行参数传递。但是,我最终得到了以下代码块: 来自conftest.py 文件: 导入 json 从 pytest 导入夹具 定义

回答 1 投票 0

使用 pytest 和 sqlalchemy 进行会话处理

对于每个单元测试,我想使用 pytest.fixture 和scoped_session 回滚已经提交的语句。 设置 蟒蛇3.11 sqlalchemy==2.0.3 pytest==7.2.1 工厂男孩==3.2.1 fastapi==0.91.0

回答 1 投票 0

Flask pytest 无法找到模块

我已经浏览了过去有关 Flask 导入问题的帖子,但我正在努力在我的特定案例中实施它们。本质上,pytest 无法识别我在

回答 1 投票 0

在 Flask 路由中模拟数据库连接和返回值

我有以下烧瓶应用程序路线: # 路线.py 从烧瓶导入 jsonify 从 app.utils.db 导入 db_connection、db_query @api.route("/some/route",methods=["GET"]) @auth 定义

回答 1 投票 0

如何在Python中有条件地跳过测试

我想在满足条件时跳过一些测试函数,例如: @skip_除非(条件) def test_method(自我): ... 这里我希望测试方法被报告为已跳过 if

回答 3 投票 0

pytest-无法访问父目录中的装置

我有以下目录结构,所有内容都在虚拟环境下运行。 - 产品目录 |- 测试目录 |- test_feature1 |- test_create.py |-conftest.py |-libdir ...

回答 1 投票 0

如果不指定 python -m pytest,Pytest 命令将无法工作

我的项目设置在 VS 代码中,与任何设置一样好。当我运行 python -m pytest testfile.py 时,一切运行顺利。但是当我尝试 pytest testfile.py 时,它会引发: ModuleNotFoundError: No ...

回答 1 投票 0

使用“@pytest.hookimpl(tryfirst=True)”会导致以下 AttributeError“生成器对象没有跳过属性”

我目前正在使用 pytest-html 和 selenium 制作一个独立的 html 报告。 顺便说一句,这是方法: @pytest.mark.hookwrapper def pytest_runtest_makereport(项目): ”“”

回答 1 投票 0

Python 3.10 中是否有相当于 setUpClass 的异步方法?

我一直在使用unittest.IsolatedAsyncioTestCase来测试我的异步方法。我一直在使用setUpClass、asyncSetUp来创建fixture并使用asyncTearDown来清理。这都是工作...

回答 1 投票 0

有没有办法在factoryboy中使用临时非字段变量?

我正在定义一些工厂来测试我的电子商务商店。我创建了一个 Faker 提供程序,它可以返回一个包含随机产品所有数据的字典。 我想要...

回答 2 投票 0

使用动态加载的 pytest 装置的可迭代/列表/生成器

我正在尝试为 pytest 创建一个动态夹具加载器,它在将值传递给测试后执行一些工作。一开始我不需要做那么复杂的事情,所以我就回来了...

回答 1 投票 0

如何使用 pytest 模拟 s3?单元测试脚本调用实际函数而不是模拟它?

我正在尝试测试一个应该模拟 s3 资源的函数,但它尝试调用实际的函数。 这是我要测试的代码。 导入系统 导入请求 导入 json 将 pandas 导入为 pd 导入

回答 1 投票 0

增加我未编写的文件的测试覆盖率

我编写了一个Python程序,用于计算给定整数列表和值范围的各种统计值。现在,我正在尝试为该程序编写一个测试文件,可以实现...

回答 1 投票 0

Pytest:在测试之外访问 addoption arg

我想在 pytest 中设置一个可选参数,并在 test_ 方法之外访问它。 我有一个 conftest.py 文件定义一个参数: def pytest_addoption(解析器): parser.addoption('--mode', ac...

回答 1 投票 0

如何使用pytest查看python中是否存在class attribute属性?

我是一名技术老师,试图学习如何使用 pytest 为我的学生创建专业的 koans 来学习 python 语法。 (如果你检查之前的问题,我有关于这里发生的事情的详细信息......

回答 1 投票 0

如何从selenium pytest中的标签中查找文本

我需要使用 Selenium、python、pytest UI 从元素中的文本中捕获付款 ID。文本的格式为“Payment 3022594”。 我正在尝试这样的事情。

回答 1 投票 0

将固定装置添加到标记的测试中

我使用 pytest 编写测试。我需要在一些测试之前和之后做某些事情。如果用 some_marker 标记,如何自动将 some_fixture 设置为测试函数? 为了实施...

回答 1 投票 0

如何使用 pytest-django 在测试之间将数据保存到数据库?

在 Django 应用程序的测试运行中使用 pytest/pytest-django 时,如何将数据保存到数据库? 我使用 py.test --nomigrations --reuse-db -s 和 Postgres DB test_ 运行 pytest 在 Django 应用程序的测试运行中使用 pytest/pytest-django 时如何将数据保存到数据库? 我使用 py.test --nomigrations --reuse-db -s 运行 pytest,并且 Postgres DB test_<configured_db_name> 按预期创建,但是在测试之间似乎没有任何内容被持久化到数据库,并且在测试运行结束时数据库为空。 import pytest from django.contrib.auth.models import User @pytest.mark.django_db(transaction=False) def test_insert_user(): user = User.objects.create_user(username="test_user", email="[email protected]", password="test") users = User.objects.all() assert len(users) > 0 @pytest.mark.django_db(transaction=False) def test_check_user(): users = User.objects.all() assert len(users) > 0 第一个测试通过了,第二个测试并没有让我怀疑是否有任何东西被持久化到数据库。根据 pytest-django 文档 @pytest.mark.django_db(transaction=False) 不会回滚受装饰测试影响的任何内容。 谢谢你, /大卫 为每个函数预填充数据库的另一种方法是这样的: import pytest from django.contrib.auth.models import User @pytest.fixture(scope='module') def django_db_setup(django_db_setup, django_db_blocker): print('setup') with django_db_blocker.unblock(): User.objects.create(username='a') assert set(u.username for u in User.objects.all()) == {'a'} @pytest.mark.django_db def test1(): print('test1') User.objects.create(username='b') assert set(u.username for u in User.objects.all()) == {'a', 'b'} @pytest.mark.django_db def test2(): print('test2') User.objects.create(username='c') assert set(u.username for u in User.objects.all()) == {'a', 'c'} 这个方法的好处是 setup 函数只调用一次: plugins: django-3.1.2 collected 2 items mytest.py setup test1 .test2 . =================== 2 passed in 1.38 seconds ==================== 不好的是,对于这样一个简单的测试来说,1.38 秒有点太多了。 --reuse-db 是一种更快的方法。 我已经解决了这个问题——为每个函数预填充数据库——通过定义一个范围为 function 的固定装置(即 model 和 session 不起作用)。 这是在 Django 中测试视图的代码。 # This is used to fill the database more easily from mixer.backend.django import mixer import pytest from django.test import RequestFactory from inventory import views from inventory import services pytestmark = pytest.mark.django_db @pytest.fixture(scope="function") def fill_db(): """ Just filling the DB with my data """ for elem in services.Search().get_lookup_data(): mixer.blend('inventory.Enumeration', **elem) def test_grid_anonymous(fill_db): request = RequestFactory().get('/grid/') response = views.items_grid(request) assert response.status_code == 200, \ "Should be callable by anyone" def test_list_anonymous(fill_db): request = RequestFactory().get('/') response = views.items_list(request) assert response.status_code == 200, \ "Should be callable by anyone"

回答 2 投票 0

HTTPException 的 Pytest 断言问题:500

我在测试一个非常简单的 FastAPI 端点时遇到问题,并且无法断言 HTTPException: 500。 这是我的端点从客户端获取 AuthenticationRequest 以从外部获取令牌

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.