当前位置:网站首页>Read JSON configuration file to realize data-driven testing
Read JSON configuration file to realize data-driven testing
2022-07-28 20:25:00 【xiaomei-Liao】
json File format :
There are three groups of data in the file , It also corresponds to three test cases : Registered successfully 、 The user name already exists 、 Message already exists .
{
"test1- Registered successfully ": {
"username": "test",
"password": "123456",
"email": "[email protected]",
"phone": "13425335433",
"question": " My favorite pet is ?",
"answer": "cat",
"exceptionresult": " Registered successfully "
},
"test2- The user name already exists ": {
"username": "test",
"password": "123456",
"email": "[email protected]",
"phone": "13425335433",
"question": " My favorite pet is ?",
"answer": "cat",
"exceptionresult": " The user name already exists "
},
"test3- Message already exists ": {
"username": "test1",
"password": "123456",
"email": "[email protected]",
"phone": "13425335433",
"question": " My favorite pet is ?",
"answer": "cat",
"exceptionresult": " The message already exists "
}
}
Read in the test case json file , Drive test execution :
① Import ddt Module package :import ddt
② Use @ddt.ddt decorator
③ Use @ddt.file_data(“json File path ”) Modify the test case method .
④ The name of the parameter of the test case method needs to be the same as json Medium key Consistent values , Otherwise, the parameter value cannot be passed correctly .
The framework used by the current test case class is unittest frame .
# coding: utf-8
import unittest
import ddt
import os
from interface.registerInterface import RegisterInterface
@ddt.ddt
class RegisterTest(unittest.TestCase):
''' register '''
@classmethod
def setUpClass(cls) -> None:
cls.url = 'http://localhost:8080/jwshoplogin/user/register.do'
cls.registerInterface = RegisterInterface(cls.url)
pass
def setUp(self) -> None:
pass
def tearDown(self) -> None:
# print(" Use case execution passes .")
pass
@classmethod
def tearDownClass(cls) -> None:
pass
@ddt.file_data(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'jsondata') + r'\registerTest.json')
def test(self, username, password, email, phone, question, answer, exceptionresult):
userinfo = {
"username": username,
"password": password,
"email": email,
"phone": phone,
"question": question,
"answer": answer
}
self.registerInterface.aw_send_register_request(userinfo)
result = self.registerInterface.aw_check_result(exceptionresult)
self.assertEqual(result, True, " Test to fail , The expected result is {0}, For the actual {1}".format(exceptionresult, result))
pass
if __name__ == '__main__':
unittest.main()
Referenced in the above class RegisterInterface class :
# coding: utf-8
import requests
'''1、username:test2 2、password:123456 3、email:[email protected] 4、phone:13549960321 5、question: My favorite pet is ? 6、answer:cat '''
class RegisterInterface(object):
def __init__(self, url):
self.url = url
self.response = None
pass
def aw_send_register_request(self, userinfo):
self.response = requests.post(self.url, data=userinfo)
# print(self.response.text)
pass
def aw_check_result(self, exception):
if exception in self.response.text:
return True
else:
if self.response.json()['data']:
return self.response.json()['data']
else:
return self.response.json()['msg']
Execute test case :
Although in RegisterTest There is only one written in the test case test Method , But the number of use cases executed is 3 individual , This is because json The data file contains 3 Group data , This is it. ddt The charm of data-driven testing , It greatly improves the reuse rate of code , And if you need to add test cases for the same request process , Only need json Add a group of test data to the file , No need to change the code .
Add a set of data to json In file :
{
"test1- Registered successfully ": {
"username": "test",
"password": "123456",
"email": "[email protected]",
"phone": "13425335433",
"question": " My favorite pet is ?",
"answer": "cat",
"exceptionresult": " Registered successfully "
},
"test2- The user name already exists ": {
"username": "test",
"password": "123456",
"email": "[email protected]",
"phone": "13425335433",
"question": " My favorite pet is ?",
"answer": "cat",
"exceptionresult": " The user name already exists "
},
"test3- Message already exists ": {
"username": "test1",
"password": "123456",
"email": "[email protected]",
"phone": "13425335433",
"question": " My favorite pet is ?",
"answer": "cat",
"exceptionresult": " The message already exists "
},
"test4- Registered successfully ": {
"username": "test1",
"password": "123456",
"email": "[email protected]",
"phone": "13425335411",
"question": " My favorite pet is ?",
"answer": "cat",
"exceptionresult": " Registered successfully "
}
}
Execution results :
边栏推荐
- WFST decoding process
- Raspberry pie 3b ffmpeg RTMP streaming
- CM4 development cross compilation tool chain production
- Raspberry connects EC20 for PPP dialing
- Does any elder brother know how to solve the huge flinksql log
- 数据挖掘(数据预处理篇)--笔记
- [C language] string reverse order implementation (recursion and iteration)
- Wildcard ssl/tls certificate
- Explain RESNET residual network in detail
- CNN convolutional neural network structure
猜你喜欢

Raspberry pie 4B parsing PWM

私有化部署的即时通讯平台,为企业移动业务安全保驾护航

CNN convolutional neural network structure

CNN convolution neural network learning process (weight update)

Solutions to the environment created by Anaconda that cannot be displayed in pycharm

Related concepts of multitasking programming

JVM (24) -- performance monitoring and tuning (5) -- Analyzing GC logs

C+ + core programming

NEIL: Extracting Visual Knowledge from Web Data

跨区域网络的通信学习静态路由
随机推荐
9. Pointer of C language (4) pointer and one-dimensional array, pointer operation
DSACTF7月re
zfoo增加类似于mydog的路由
Solve the brick stacking problem (DP)
Raspberry Pie 3 connected to WiFi
Solutions to the environment created by Anaconda that cannot be displayed in pycharm
[experiment sharing] CCIE BGP reflector experiment
2. Floating point number, the difference between float and double in C language and how to choose them
C language - data type
C+ + core programming
Solve the kangaroo crossing problem (DP)
C language - pointer
Torch. NN. Linear() function
弹出模态框
Introduction to seven kinds of polling (practice link attached)
Related concepts of multitasking programming
Store and guarantee rancher data based on Minio objects
树行表达方式
跨区域网络的通信学习静态路由
Durham High Lord (classic DP)