我使用 pytest 调用目录下的所有测试。我怎样才能最后运行一个特定的测试用例?
python -m pytest ../testdir
../testdir/test_1.py.... test_n.py
Run Code Online (Sandbox Code Playgroud) 我想从 Python 调用返回 Python 对象的 Rust 函数:
my_rust_module.my_function() # => <object>
Run Code Online (Sandbox Code Playgroud)
我不确定如何创建这个函数,因为类实例化的 PYO3 指南描述了如何将这样的对象实例化为 a PyRef,但是我不知道如何从 a 返回这样的引用pyfunction。
这就是我正在尝试的:
#[pyfunction]
fn my_function(py: Python) -> PyRef {
let gil = Python::acquire_gil();
let py = gil.python();
PyRef::new(py, MyStruct { }).unwrap()
}
Run Code Online (Sandbox Code Playgroud)
但是,PyRef似乎不是有效的返回类型(编译器说“类型参数的数量错误:预期为 1,发现为 0”),并且我不知道如何将 a 转换PyRef为可以返回的内容,例如 a PyObject。
如何设置 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)
感谢您的帮助!
我正在尝试连接到我的postgres数据库。
一切正常,但是在我的数据库中,我有2个模式。默认情况下,其中一个是由PgAdmin(public)创建的,第二个是ZTB我创建的。
当我使用NodeJS连接到数据库时,默认情况下它将连接到“ public”架构,因此无法从“ ZTB”查询表。
有什么解决方案可以切换NodeJS服务器中的当前工作模式?
问候
我知道这违反了任何需要/假设正确打包 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) 我想写一些测试用例来练习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
我需要向live_server由pytest-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 脚本时,我们可以使用sys.argv获取命令行参数并在我们的 python 代码中的任何地方使用它。
在运行 pytest 时,我们可以使用pytest_addoption添加命令行参数,但在我看来,我们只能在测试或固定装置中使用它们,我们可以通过任何公开config对象的方式访问选项。
但是,就我而言,我希望能够从测试模块主体本身访问命令行选项。
是否可以在不需要任何夹具的情况下以某种方式访问 pytest 的配置?
我需要一些有关使用setuptools和find_packages功能的解释。\n我有一个这样的项目结构:
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 project_dir_1\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 module.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 my_project\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 cli.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 subdir1\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 module.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 conf\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 module.py\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 params\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 config.yml\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 subdir2\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 module.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 version.py\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 project_dir_2\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 subdir1\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 module.py\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.md\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 requirements.txt\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 setup.py\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 tests\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test_main.py\nRun Code Online (Sandbox Code Playgroud)\n\n实际上,我的所有代码都在该my_project目录中,而且我还有两个附加目录project_dir_1,project_dir_2其中包含必要的外部模块,我应该在包代码和另一个项目代码中导入这些模块,该包将安装在 venv 中。 \n我有安装脚本像这样:
setup(\n name=\'my_project\',\n version=\'0.0.1\',\n description=\'Python …Run Code Online (Sandbox Code Playgroud) 阅读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 ×7
pytest ×6
django ×1
graphql ×1
node.js ×1
packaging ×1
postgresql ×1
pyo3 ×1
python-3.x ×1
rust ×1
schema ×1
setup.py ×1
setuptools ×1
sqlalchemy ×1
testing ×1