Foreword
ddt: data-driven, said the simple point is that multiple sets of test data, such as when little point login input normal, abnormal data log.
The actual project, the automated test with very little, but it was also used to maintain test data excel
A, ddt
1, the installation: pip install ddt (here I've installed a)
C:\Users\Administrator>pip install ddt Requirement already satisfied: ddt in d:\path_python\lib\site-packages (1.2.1)
2, look at our previous normal login mass participation of Zen. Login required account number and password, and now only one set of data, ddt can do multiple sets of data execution. (Here let yourself come)
# Log S = requests.session () LOGIN_URL = ' http://127.0.0.1/zentao/user-login.html ' PR = { ' Account ' : ' ADMIN ' , ' password ' : ' 123456 ' , } login_r = s.post (LOGIN_URL, params = PR) # pass params parameter # say whether the login is successful r1 = S. GET ( ' http://127.0.0.1/zentao/doc-browse-1-byModule-0-id_desc-doc. HTML ' ) IF ' products Main Library ' in r1.content.decode ( ' UTF-8 ' ): Print ( ' Login successful ' ) the else : Print ( ' Login failed ' )
3, to be introduced ddt, unittest.
① first of all have multiple sources of data sets (I wrote a set of data json own definition, you can also read the file, like like !!!)
② test class, we need to add a decorator: @ ddt.ddt
③ test function, also need to add a decorator and transfer all of the data over the entire data transfer * respectively: @ ddt.data (* test_data)
④ Note: sets of data used in ddt pass that data, rather than test_data, but not * test_data
import requests, ddt, unittest data # ddt need to prepare the data driver TEST_DATA = [ { ' Account ' : ' ADMIN ' , ' pwd ' : ' 123465 ' , ' exp ' : ' ADMIN ' }, { ' Account ' : ' gsxl ' , ' pwd ' : ' 123321 ' , ' exp ' :'gsxl'}, {'account': 'xxxx', 'pwd': '654321', 'exp': 'xxxx'}, ] # 类需要ddt装饰器 @ddt.ddt class Test_xl(unittest.TestCase): def setUp(self): self.s = requests.session() self.s.verify = False def tearDown(self): self.s.close() @ddt.data(*test_data) # use cases need to use a test data ddt DEF test_001 (Self, Data): Print ( ' ddt test data:% S ' % Data) IF the __name__ == ' __main__ ' : unittest.main () # told my interpreter is run by the unittest !!!
4, how to get to the corresponding values you want to do? It's like the return of dict, list, as it can get to, otherwise they first learn the new structure of the underlying data deletion check changed. Simple extraction as follows:
import requests, ddt, unittest # ddt数据驱动所需准备的数据 test_data = [ {'account': 'admin', 'pwd': '123465', 'exp': 'admin'}, {'account': 'gsxl', 'pwd': '123321', 'exp': 'gsxl'},] # 类需要ddt装饰器 @ddt.ddt class Test_xl(unittest.TestCase): def setUp(self): self.s = requests.session() self.s.verify = False def tearDown(self): self.s.close() @ddt.data(*test_data) # 某个用例需要用到ddt的测试数据 def test_001(self, data): # print('ddt测试数据是:%s' % data) user = data['account'] pwd = data['pwd'] exp = data['exp'] print('这是账号:', user) print('这是密码:', pwd) print('这是期望结果:', exp) if __name__ == '__main__': unittest.main() # 告诉解释器我是用unittest运行的!!!