python 2.7中类似子测试的功能?
如何在Python2.7中编写下面这样的测试用例?我没有使用Pytest/Python3的选项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'), ]
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()
如果我想单独测试每个断言,我将不得不将每个断言放在自己的方法中,这将很麻烦 使用,它支持许多较新的功能。可能重复