python - 如何修复 PytestAssertRewriteWarning?

标签 python pytest

我刚刚开始使用 pytestpytest_mysql 并拥有以下代码:

import pytest
from pytest_mysql import factories

if __name__ == "__main__":
    pytest.main([r'test.py', '-v'])
    testdb = factories.mysql('testdb')

这会生成警告:

PytestAssertRewriteWarning: Module already imported so cannot be rewritten: pytest_mysql
  1. 我应该担心吗?
  2. 如何确保我不会收到警告?

最佳答案

这就是我解决它的方法!

import pytest
if __name__=='__main__': #Note 1
    pytest.main()
#
#Your code goes here
#
from pytest_mysql import factories
testdb = factories.mysql('testdb')

因此,在导入其他内容之前尝试运行 pytest.main() ..

注1

这不是您通常调用__name__=='__main__'的方式,但因为它有效,所以我买了它!

关于python - 如何修复 PytestAssertRewriteWarning?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63037729/

相关文章:

python - 属性错误 : 'NoneType' object has no attribute '_meta'

python - 使用 numpy 的 python 中的多线性映射

模型类的 Python 单元测试

python - py.test 多次测试不同结果

python - 从 args 动态设置 pytest fixture 的范围

python - 将生成器包装到缓冲区中?

python - 从 Django 模板标签的变量中分割字符串

python - 在写入 HTML 文件之前尝试用一个变量替换另一个变量

python - 跨多个测试脚本模拟 Python 模块

python - 带有自定义 id 函数的 pytest 参数化测试