Python unittest,在使用base-test-class时跳过测试

时间:2013-12-16 17:08:05

标签: python python-unittest

在测试我们的某个网络应用程序时,为了清晰起见,我创建了一个继承BaseTestClass的{​​{1}}。 unittest.TestCase包含我的BaseTestClasssetUp()方法,我的每个tearDown()类都会继承这些方法。

由于测试中的不同设备具有相似的页面并存在一些差异,我想使用<Page>Test装饰器,但证明它很难。如果我尝试使用该装饰器,Eclipse会尝试将@unittest.skipIf()自动导入BaseTestClass,而不是“继承”unittest.TestCase中的装饰器,这对我来说似乎不对。

使用<Page>Test时,有没有办法使用skip修饰符?

Base

单独模块中的一个测试类:

class BaseTestClass(unittest.TestCase):

    def setUp():
        #do setup stuff
        device = "Type that blocks"

    def tearDown():
        #clean up

1 个答案:

答案 0 :(得分:3)

显然这需要unittest2(或Python 3,我假设),但除此之外,你的例子非常接近。确保您的单元测试发现机制(test_*.py为鼻子)发现您的真实测试代码的名称。

#base.py
import sys
import unittest2 as unittest

class BaseTestClass(unittest.TestCase):

    def setUp(self):
        device = "Type that blocks"
    def tearDown(self):
        pass

在实际代码中:

# test_configpage.py
from base import *

class ConfigPageTest(BaseTestClass):

    def test_one(self):
        pass

    def test_two(self):
        pass

    @unittest.skipIf(True, 'msg')
    def test_three(self):
        pass

给出输出

.S.
----------------------------------------------------------------------
Ran 3 tests in 0.016s

OK (SKIP=1)