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

Mar*_*nds 6 python python-unittest

在测试我们的一个网络应用程序时,为了清晰起见,我创建了一个BaseTestClass继承的unittest.TestCase.在BaseTestClass包括我setUp()tearDown()方法,其中每个我的<Page>Test类,然后继承.

由于测试中的不同设备具有相似的页面并且存在一些差异,因此我想使用@unittest.skipIf()装饰器,但其证明是困难的.而不是"继承"了从装饰BaseTestClass,如果我尝试使用Eclipse的装饰试图自动导入unittest.TestCase<Page>Test,这看起来不正确我.

有没有办法在使用skip时使用装饰器Base

class BaseTestClass(unittest.TestCase):

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

    def tearDown():
        #clean up
Run Code Online (Sandbox Code Playgroud)

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

class ConfigPageTest(BaseTestClass):

    def test_one(self):
        #do test

    def test_two(self):
        #do test

    @unittest.skipIf(condition, reason) <<<What I want to include
    def test_three(self):
        #do test IF not of the device type that blocks
Run Code Online (Sandbox Code Playgroud)

And*_*ker 3

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

#base.py
import sys
import unittest2 as unittest

class BaseTestClass(unittest.TestCase):

    def setUp(self):
        device = "Type that blocks"
    def tearDown(self):
        pass
Run Code Online (Sandbox Code Playgroud)

在实际代码中:

# 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
Run Code Online (Sandbox Code Playgroud)

这给出了输出

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

OK (SKIP=1)
Run Code Online (Sandbox Code Playgroud)