python - py.test 将 IndexError/KeyError 报告为失败而不是错误

标签 python python-3.x pytest python-unittest

 import unittest
 import logging

 FORMAT = '%(asctime)-15s %(message)s'
 logging.basicConfig(format=FORMAT)


 log = logging.getLogger(__name__)
 log.root.setLevel(logging.DEBUG)

 class FixturesTest(unittest.TestCase):

     def setUp(self):
         log.info('starting setup')
         self.fixture = range(1, 10)
         log.warn('Hello world')

     def tearDown(self):
         log.info('starting teardown')
         del self.fixture
         log.info('Goodbye world')

     def test_pass(self):
         log.info('in test pass')
         self.assertEqual(self.fixture, range(1, 10))
         log.error('Test world')

     def test_fail(self):
         log.info('in test fail')
         log.error("let's fail a test")
         assert(1==0)

     def test_error(self):
         log.info('in test error')
         log.info("let's error a test")
         d = []
         d[1]

     if __name__ == '__main__':
         unittest.main()

使用 py.test 运行上面的代码,它将 test_error 测试方法报告为测试失败而不是测试错误。而使用 unittest 运行它会将 test_error 显示为错误而不是失败,您能分享一下对此的任何想法吗?

更新:相同的代码,nose 也将其视为错误而不是 test_error 的失败,这是 py.test 中的问题吗?

更新:如果你不确定我指的是什么,如果你运行 py.test test_filename.py,你会得到 1 次通过,2 次失败,如果你运行 python -m unittest test_filename.py 或 nosetest test_filename.py,这是 1 次通过,1 次失败,1 次错误。

最佳答案

pytest 不区分测试失败(由于AssertionError)和测试错误(任何其他异常),如 unittest 确实如此。这不是错误,而是设计使然。

this thread 中有一些讨论添加对这种类型的分离的支持,结果是 pytest-finer-verdicts插入。

关于python - py.test 将 IndexError/KeyError 报告为失败而不是错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41272327/

相关文章:

python - macOS Sierra/Python2.7.13 URLError : <urlopen error EOF occurred in violation of protocol (_ssl. c:661)>

python - 在 CMake 中使用 setup.py 构建一个 python 包

python - 在 Python 中读取 PDF 属性/元数据

python - 如何使用反向正确执行 HttpResponseRedirect?

python - 类型提示返回 NameError : name 'datetime' not defined

python - Python 支持的 IDE 无法捕获缺失的属性

python - 限制 pytest 仅运行 pylint 而不是单元测试

python - Pytest:集合错误,函数不使用参数 'date'

Python 碎片 : How to return nothing if xpath doesn't exist?

visual-studio-code - 在 shell 中运行 vscode 命令与输出中显示的命令有什么区别?