繁体   English   中英

py.test:programmaticalyl忽略单元测试

[英]py.test: programmaticalyl ignore unittest tests

我正在努力促进我们的团队从unittest迁移到py.test,希望减少样板工作和更快的运行速度可以减少关于为什么他们编写的单元测试不如预期的指控。

我们django.unittest.TestCase一个问题是,几乎所有旧django.unittest.TestCase都由于错误而失败。 另外,它们中的大多数确实很慢。

我们已经决定,新测试系统将忽略旧测试,而仅用于新测试。 我试图通过在conftest.py创建以下内容来使py.test忽略旧测试:

def pytest_collection_modifyitems(session, config, items):
  print ("Filtering unittest.TestCase tests")
  selected = []
  for test in items:
    parent = test.getparent(pytest.Class)
    if not parent or not issubclass(parent.obj, unittest.TestCase) or hasattr(parent.obj, 'use_pytest'):
        selected.append(test)

  print("Filtered {} tests out of {}".format(len(items) - len(selected), len(items)))
  items[:] = selected

问题是,它过滤了所有测试,也有一个:

import pytest


class SanityCheckTest(object):

  def test_something(self):
    assert 1

为新测试使用一些不同的命名模式将是一个很差的解决方案。

我的测试类不符合命名约定。 我将其更改为:

import pytest


class TestSanity(object):

  def test_something(self):
    assert 1

并且还修复了我的pytest_collection_modifyitems的错误,它可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM