paramunittest参数化测试基础
samples:
import paramunittest
import unittest
@paramunittest.parametrized(
(10,20),
(30,40),
# (100,20)
)
class ApiTestDemo(paramunittest.ParametrizedTestCase): #unittest.TestCase
def setParameters(self, numa,numb):
self.a = numa
self.b = numb
def test_add_case(self):
print( '%d+%d?=%d'%(self.a,self.b,30) )
self.assertEqual( self.a + self.b,30 )
if __name__=='__main__':
unittest.main(verbosity=2)
前面定义的是字典,那参数就跟前面字典的key保持一致.
demo_09.py
import paramunittest
import unittest
@paramunittest.parametrized(
{'numa':10,'numb':30},
{'numa':40,'numb':50}
)
class ApiTestDemo(paramunittest.ParametrizedTestCase):
def setParameters(self, numa, numb):
self.a = numa
self.b = numb
def test_add(self):
print('%d+%d?=%d' % (self.a, self.b, 40))
self.assertEqual(self.a + self.b, 40)
if __name__=='__main__':
unittest.main(verbosity=2)
把数据移出去:
参考:
demo_10.py
import paramunittest
import unittest
test_data = [{'numa':10,'numb':30},{'numa':40,'numb':50}]
def get_data():
return test_data
@paramunittest.parametrized(
*get_data()
)
class ApiTestDemo(paramunittest.ParametrizedTestCase):
def setParameters(self, numa, numb):
self.a = numa
self.b = numb
def test_add(self):
print('%d+%d?=%d' % (self.a, self.b, 40))
self.assertEqual(self.a + self.b, 40)
if __name__=='__main__':
unittest.main(verbosity=2)
回忆滋润坚持