Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python 2.7中类似子测试的功能?_Python_Python 2.7_Python Unittest_Parameterized Unit Test - Fatal编程技术网

python 2.7中类似子测试的功能?

python 2.7中类似子测试的功能?,python,python-2.7,python-unittest,parameterized-unit-test,Python,Python 2.7,Python Unittest,Parameterized Unit Test,如何在Python2.7中编写下面这样的测试用例?我没有使用Pytest/Python3的选项 import unittest class TestStringMethods(unittest.TestCase): def test_upper(self): tests = [ ('foo', 'FOO'), ('too', 'TOO'), ('poo', 'POO'), ]

如何在Python2.7中编写下面这样的测试用例?我没有使用Pytest/Python3的选项

import unittest

class TestStringMethods(unittest.TestCase):
    def test_upper(self):
        tests = [
            ('foo', 'FOO'),
            ('too', 'TOO'),
            ('poo', 'POO'),
        ]
        for value, expected in tests:
            with self.subTest(value=value):
                self.assertEqual(value.upper(), expected)

if __name__ == '__main__':
    unittest.main()
总之,我正在寻找Python2.7中“Pytest中的参数化fixture”和“unittest的子测试特性”的替代品

我可以在Python2.7中执行以下操作,但第一个失败的断言将退出测试。因为我的测试用例中只有一个方法,所以只有一个测试运行——尽管一个测试评估了许多断言

class TestStringMethods(unittest.TestCase):
    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')
        self.assertEqual('too'.upper(), 'TOO')
        self.assertEqual('poo'.upper(), 'POO')

if __name__ == '__main__':
    unittest.main()
如果我想单独测试每个断言,我将不得不将每个断言放在自己的方法中,这将很麻烦

使用,它支持许多较新的功能。可能重复