小编hoe*_*ing的帖子

在 graphene-sqlalchemy 查询中按 id 过滤

如何设置 graphene-sqlalchemy 按 id 过滤对象?

我想运行查询:

{
  marker(markerId: 1) {
    markerId
    title
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望获得一个 MarkerId 为 1 的 Marker 对象,但收到错误““Query”类型的“marker”字段上的未知参数“markerId””。

我有两个文件:

模式.py

{
  marker(markerId: 1) {
    markerId
    title
  }
}
Run Code Online (Sandbox Code Playgroud)

模型.py

import graphene
from graphene_sqlalchemy import SQLAlchemyObjectType
from model import db_session, Marker as MarkerModel

class Marker(SQLAlchemyObjectType):
    class Meta:
        model = MarkerModel

class Query(graphene.ObjectType):
    marker = graphene.Field(Marker)
    markers = graphene.List(Marker)

    def resolve_markers(self, args, context, info):
        return db_session.query(MarkerModel).all()

    def resolve_marker(self, args, context, info):
        return db_session.query(MarkerModel).first()

schema = graphene.Schema(query=Query)
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助!

sqlalchemy graphql graphene-python

5
推荐指数
1
解决办法
4700
查看次数

Pytest“run-around-tests”装置在一个类中的所有测试之前只运行一次

我正在使用 pytest + selenium 测试 Web 解决方案的用户消息功能。测试将向测试用户生成测试消息,然后登录该用户以验证该消息确实为该用户显示。

  • 我需要通过内部 API 生成这些消息。
  • 为了能够访问这个 API,我首先必须通过不同的 API 生成一个 AUTH 令牌。

所以测试场景基本上是:

  1. 在测试启动时,通过 API 辅助函数生成新的 AUTH 令牌。
  2. 向另一个 API 发送请求以设置新消息(需要 AUTH 令牌)
  3. 向另一个 API 发送请求以将此消息“映射”到指定用户(需要 AUTH 令牌)
  4. 登录测试用户并验证新消息是否确实显示。

我的问题是我想避免每次运行我的测试类中的每个测试时都创建一个新的 AUTH 令牌 - 一旦所有测试在同一个测试运行中使用,我想创建一个新令牌。

在调用所有测试时生成一个新访问令牌的最聪明的解决方案是什么?

现在我想出了这样的东西,每次运行任何单独的测试时都会生成一个新的令牌:

import pytest
import helpers.api_access_token_helper as token_helper
import helpers.user_message_generator_api_helper as message_helper
import helpers.login_helper as login_helper
import helpers.popup_helper as popup_helper

class TestStuff(object):

    @pytest.yield_fixture(autouse=True)
    def run_around_tests(self):
        yield token_helper.get_api_access_token()

    def test_one(self, run_around_tests):
        auth_token = run_around_tests
        message_helper.create_new_message(auth_token, some_message_data)
        message_helper.map_message_to_user(auth_token, user_one["user_id"])
        login_helper.log_in_user(user_one["user_name"], user_one["user_password"])
        assert popup_helper.user_message_is_displaying(some_message_data["title"])

    def …
Run Code Online (Sandbox Code Playgroud)

python selenium automated-tests pytest

5
推荐指数
1
解决办法
4121
查看次数

如何使用pytest运行与生产代码位于同一文件中的测试?

我知道这违反了任何需要/假设正确打包 Python 生产代码的最佳实践:在某些情况下,能够在同一文件中定义生产和测试代码可能会有所帮助(例如,在简单脚本的情况下)。如何使用pytestthen在文件中运行所有或特定测试?

编辑- 我的特定用例的解决方案:

中的文件结构<root>

pytest.ini
scrip_with_tests.py
Run Code Online (Sandbox Code Playgroud)

内容pytest.ini

[pytest]
python_files = script_with_tests.py
Run Code Online (Sandbox Code Playgroud)

内容script_with_tests.py

import pytest  # this is not required, works without as well

def test_always_pass():
    pass

if __name__ == "__main__":
    main()
Run Code Online (Sandbox Code Playgroud)

pytest调用<root>

pytest script_with_tests.py
Run Code Online (Sandbox Code Playgroud)

python testing pytest

5
推荐指数
1
解决办法
921
查看次数

如何在python中将数据模拟为request.Response类型

我想写一些测试用例来练习isinstance(obj, requests.Response) 逻辑中的object_check。在我创建 Mock 数据作为 requests.post 的返回值之后。模拟数据的类型始终是 Mock 类。这样,我如何重写模拟数据,以便模拟数据可以是 requests.Response 类型?所以我可以锻炼线d = obj.json()

from unittest.mock import patch, Mock
import unittest
import requests
from requests.exceptions import HTTPError
import pytest
def object_check(obj):
    if isinstance(obj, bytes):
        d = ujson.decode(obj.decode())
    elif isinstance(obj, requests.Response):
        d = obj.json()
    else:
        raise ValueError('invalid type')
    return d

def service_post(params):
    """
    trivial function that does a GET request
    against google, checks the status of the
    result and returns the raw content
    """
    url = "https://www.iamdomain.com"
    params …
Run Code Online (Sandbox Code Playgroud)

python pytest python-3.x python-unittest python-unittest.mock

5
推荐指数
1
解决办法
1万
查看次数

我应该如何登录我的 pytest 插件?

我正在构建一个 pytest 插件,目前我通过在插件文件的顶部添加类似于以下两行的内容来登录:

logging.basicConfig()
log = logging.getLogger(PLUGIN_NAME)
Run Code Online (Sandbox Code Playgroud)

但是,这似乎没有考虑 pytest 配置(通过命令行选项或 ini 文件),例如日志文件路径、记录器格式等。

我可以手动将它们复制到我的basicConfig电话中,但我认为 pytest 和第三方插件之间有更好的工具用于日志记录,不是吗?

我在 pytest 的文档和代码中发现了相当多的日志记录参考,但所有这些都在讨论捕获测试日志。我对记录pytest 插件本身很感兴趣。

python testing logging pytest

5
推荐指数
1
解决办法
965
查看次数

Pytest无法通过标记skipif跳过类中的testcase

我正在使用pytest框架,并希望根据某些条件跳过testcase.下面的代码不会跳过测试用例.

import pytest
class TEST:
    @pytest.fixture(scope="function", autouse=True)
    def func_fixture(self):
        return "fail"

    @pytest.mark.skipif("self.func_fixture=='fail'")
    def test_setting_value(self):
        print("Hello I am in testcase")
Run Code Online (Sandbox Code Playgroud)

运行时,它表示0个测试用例已执行.

func_fixture对于测试套件非常重要.它在开始测试之前执行许多先决条件.

如果我删除类,并使用相同的语法添加其余的函数(删除self后),它可以工作.不知道为什么它在课堂上失败了

python testing unit-testing pytest

5
推荐指数
1
解决办法
573
查看次数

pytest-django 将夹具添加到 live_server 夹具

我需要向live_serverpytest-django专门覆盖的django_db_setup.

话虽如此,我知道对未刷新干净的数据库运行测试并不理想,但这是我正在使用的。

在我们的正常测试套件中,我们使用覆盖django_db_setup在我们的conftest.py文件中不做任何事情,如下所示

@pytest.fixture(scope="session")
def django_db_setup():
    pass
Run Code Online (Sandbox Code Playgroud)

看来,当我使用它live_server提供的夹具时,pytest-django它不尊重这一点,因为它试图在测试结束时刷新数据库。人们将如何规避这一点?我发现了如下所示的结束,但如果有更好的解决方案,我想避免它。

@pytest.fixture(scope='session')
def my_live_server(request):
    request.getfixturevalue('django_db_setup')
    return live_server(request)
Run Code Online (Sandbox Code Playgroud)

python django pytest pytest-django

5
推荐指数
1
解决办法
1102
查看次数

如何为pytest命令指定几个标记

阅读http://doc.pytest.org/en/latest/example/markers.html我看到了基于标记包含或排除某些 python 测试的示例。

包含:

pytest -v -m webtest
Run Code Online (Sandbox Code Playgroud)

不包括:

pytest -v -m "not webtest"
Run Code Online (Sandbox Code Playgroud)

如果我想为包含和排除指定多个标记怎么办?

python pytest

5
推荐指数
1
解决办法
496
查看次数

为什么Python的filterwarning模块正则表达式不能按预期工作?

根据Python警告文档, filterwarnings模式是action:message:category:module:line,并且该module部分应该允许根据正则表达式匹配特定模块。我试图让这项工作来过滤第三方库模块的特定警告,但该功能似乎不起作用。

最小的例子

重现问题的更简单方法是创建一个文件

my_module.py

print("\d")
Run Code Online (Sandbox Code Playgroud)

然后PYTHONWARNINGS="error,ignore:::.*" python -c "import my_module"从同一目录运行。显然,正则表达式.*应该与模块名称匹配my_module,但不知何故却没有。问题是为什么?

原始示例

举个例子:导入包(机器人操作系统(ROS)rosbag发行版附带的一些包)在“严格”report-warnings-as-errors 模式下运行时会触发错误:

print("\d")
Run Code Online (Sandbox Code Playgroud)

这是有道理的,它应该使用原始字符串文字。

现在我尝试使用模块正则表达式来消除来自rosbag模块的警告。出于示例的目的,我通过指定过滤器警告,PYTHONWARNINGS但其他方法(例如通过 pytests 设置)会导致相同的行为。

我收到以下意外行为:

  • 使用PYTHONWARNINGS='error,ignore:invalid escape sequence::'作品。当然,这会过滤所有“无效转义序列”警告。因此,让我们module在末尾添加一个正则表达式以特定于模块......
  • usingPYTHONWARNINGS='error,ignore:invalid escape sequence::rosbag.*'应该完全做到这一点,但过滤器似乎不匹配。警告仍然被报告为错误,就像普通的一样error
  • 即使PYTHONWARNINGS='error,ignore:::.*'理论上应该匹配任何模块的 using 也无法匹配。仅像匹配一样完全删除正则表达式PYTHONWARNINGS='error,ignore:::',但当然这本质上相当于只是PYTHONWARNINGS='ignore'.

任何想法为什么模块正则表达式根本不匹配?!这是 Python 发行版的错误吗?我使用的是 Python 3.8.10 / Ubuntu 20.04。

python warnings

5
推荐指数
1
解决办法
600
查看次数

pytest-django:这是使用参数测试视图的正确方法吗?

假设我正在Django应用程序中测试RSS feed视图,这是我应该怎么做的吗?

def test_some_view(...):
    ...
    requested_url = reverse("personal_feed", args=[some_profile.auth_token])
    resp = client.get(requested_url, follow=True)
    ...
    assert dummy_object.title in str(resp.content)
Run Code Online (Sandbox Code Playgroud)
  1. reverse-ing然后将其传递client.get()给正确的测试方法?我认为这比以前简单地.get()设置URL 更干燥,而且更适合未来使用。

  2. 我应该断言这dummy_object就是这种回应吗?

  3. 我在这里使用str响应对象的表示形式进行测试。与使用时相比,selenium什么时候是个好习惯?我知道,例如,可以更轻松地验证obj或属性(如dummy_object.title)是否封装在H1标签中。在另一方面,如果我不关心如何与obj表示,它的速度更快做像上面。

python django django-testing pytest pytest-django

4
推荐指数
1
解决办法
382
查看次数