简体   繁体   中英

py.test: programmaticalyl ignore unittest tests

I'm trying to promote our a team to migrate to py.test from unittest in hope that less boilerplate and faster runs will reduce accuses as to why they don't write as much unittests as they should.

One problem we have is that almost all of our old django.unittest.TestCase fail due to errors. Also, most of them are really slow.

We had decided that the new test system will ignore the old tests, and will be used for new tests only. I tried to get py.test to ignore old tests by creating the following in conftest.py :

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

Problem is, it filters all tests, also this one:

import pytest


class SanityCheckTest(object):

  def test_something(self):
    assert 1

Using some different naming pattern for the new tests would be a rather poor solution.

My test class did not conform to the naming convention. I change it to:

import pytest


class TestSanity(object):

  def test_something(self):
    assert 1

And also fixed a bug in my pytest_collection_modifyitems and it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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