我为 celery 编写了以下最小单元测试类
import pytest
@pytest.fixture
def celery_config():
return {
"broker_url": "redis://localhost:6379/0",
"result_backend": "redis://localhost:6379/0"
}
@pytest.mark.celery(result_backend="redis://")
class GetHash:
def test_some(self):
pass
我有 celery version 4.3.0
和 PyTest
及其插件如下
pytest==5.1.1
pytest-black==0.3.7
pytest-cov==2.7.1
pytest-forked==1.0.2
pytest-runner==5.1
pytest-xdist==1.29.0
尝试测试时出现以下错误
test_get_hash.py:12: in <module>
@pytest.mark.celery(result_backend="redis://")
/home/work/.virtualenvs/dev_env/lib/python3.6/site-packages/_pytest/mark/structures.py:324: in __getattr__
PytestUnknownMarkWarning,
E pytest.PytestUnknownMarkWarning: Unknown pytest.mark.celery - is this a typo?
包列表
amqp==2.5.1
anyjson==0.3.3
apipkg==1.5
appdirs==1.4.3
atomicwrites==1.3.0
attrs==19.1.0
autoflake==1.3
Babel==2.7.0
bandit==1.6.2
billiard==3.6.1.0
black==19.3b0
celery==4.3.0
Cerberus==1.3.1
certifi==2019.6.16
chardet==3.0.4
checksumdir==1.1.6
Click==7.0
coverage==4.5.3
execnet==1.6.0
Flask==1.0.2
Flask-Cors==3.0.8
flower==0.9.3
gitdb2==2.0.5
GitPython==2.1.13
idna==2.8
importlib-metadata==0.19
isort==4.3.20
itsdangerous==1.1.0
Jinja2==2.10.1
kombu==4.6.4
MarkupSafe==1.1.1
mock==3.0.5
more-itertools==7.0.0
mysql-connector-python==8.0.16
Nuitka==0.6.5
packaging==19.1
pbr==5.4.2
pluggy==0.12.0
protobuf==3.7.1
py==1.8.0
pyflakes==2.1.1
pyparsing==2.4.2
pytest==5.1.1
pytest-black==0.3.7
pytest-cov==2.7.1
pytest-forked==1.0.2
pytest-runner==5.1
pytest-xdist==1.29.0
python-dateutil==2.8.0
python-dotenv==0.10.1
pytz==2019.2
PyYAML==5.1.2
redis==3.3.8
requests==2.22.0
rq==1.1.0
six==1.12.0
smmap2==2.0.5
SQLAlchemy==1.3.3
stevedore==1.30.1
toml==0.10.0
tornado==5.1.1
urllib3==1.25.3
vine==1.3.0
wcwidth==0.1.7
Werkzeug==0.15.2
我需要为此安装一个特殊的插件来修复错误吗?
最佳答案
您看到的是警告,而不是错误,除非您enabled strict markers .你可以忽略这个,或者告诉 pytest 不要向你显示它们(使用 --disable-warnings
)或者忽略这个特定的警告(使用 -W ignore::pytest.PytestUnknownMarkWarning
)。
您也可以register the marker ,将其添加到您的 pytest.ini
文件中:
[pytest]
markers =
celery(**overrides): override celery configuration for a test case
celery pytest 插件将获取您使用标记所做的特定于测试的配置。
这些警告是 introduced in Pytest 4.5 ,理想情况下 Celery 项目应该 pre-register the marker在他们的插件代码中。我有 added this to your Celery issue report .
我也made a pull request这使得 celery 添加注册。不久之后就合并了,所以 upcoming Celery 4.4.0 release将使上述内容过时。
关于python - 为 celery 任务编写测试时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57846173/