python - 如何参数化python unittest setUp方法?

标签 python nosetests unit-testing

我正在尝试使用不同的设置方法运行相同的测试用例。我试过使用 nosetests 和参数化,但它似乎不支持参数化 setUp 方法。这是我正在尝试做的一个例子:

...
from nose_parameterized import parameterized

class Example(unittest.TestCase):

    @parameterized.expand(['device1', 'device2'])
    def setUp(self, device):
        desired_caps = {}
        desired_caps['key1'] = device
        desired_caps['key2'] = 'constant value'

    self.driver = webdriver.Remote(url, desired_caps)

    def tearDown(self):
        self.driver.quit()

    def test_app_launch(self):
        # assert something

错误是:TypeError: setUp() takes exactly 2 arguments (1 given)

有没有其他方法可以参数化 setUp 方法?我还研究了 nosetests 生成器,但它似乎也不是可行的方法。

最佳答案

所以我的方法是设置一个基础测试,其中包含设备必须通过的所有测试。然后,您必须对从该 baseTest 继承的 deviceTests 使用它们自己的特定于设备的额外设置。

# this is the base test. Everything that is not specific to the device is set up here. It also contains all the testCases.
import unittest
class deviceTest( unittest.TestCase ):

  def setUp( self ):
    '''
    General setUp here
    '''
    self.desired_caps = {}
    self.desired_caps['key2'] = 'constant value'

  def testWorkflow( self ):
    '''
    Here come the tests that the devices have to pass
    '''

class device1Test( deviceTest ):

  def setUp( self ):
    '''
    device1 specific setup
    '''
    #also run general setUp    
    deviceTest.setUp( self )
    self.desired_caps['key1'] = device
    self.driver = webdriver.Remote(url, desired_caps)


class device2Test( deviceTest ):

  def setUp( self ):
    '''
    device2 specific setup
    '''
    #also run general setUp
    deviceTest.setUp( self )
    self.desired_caps['key1'] = device
    self.driver = webdriver.Remote(url, desired_caps)


if __name__ == '__main__':
  suite = unittest.defaultTestLoader.loadTestsFromTestCase( device1Test )
  suite.addTest( unittest.defaultTestLoader.loadTestsFromTestCase(device2Test ) )
  unittest.TextTestRunner( verbosity = 2 ).run( suite )

关于python - 如何参数化python unittest setUp方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28695276/

相关文章:

python - 在不同条件下运行一组标准的nosetests函数

javascript - JEST 和 ES6 导入 - 基于根文件夹的导入不起作用

c++ - 发送到应用程序的 CTRL-C 的单元测试

python - 如何迭代不同数据帧中的行并将其用作其他数据帧中的值?

python - 如何将自定义 nose 插件添加到 `nosetests` 命令

python - 在 autospeccing 时模拟 side_effect 为函数提供额外的参数

python - 配置 NoseTests 查找的目录

unit-testing - 在没有导出的情况下测试 typescript 模块

python - 没有numpy的矩阵转置,错误: list index out of range

python - 如何使用正则表达式搜索字符串中的 2-9 位数字?