簡體   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