当前位置:网站首页>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 :
边栏推荐
- Raspberry pie creation self start service
- Commands related to obtaining administrator permissions
- Explain RESNET residual network in detail
- 9. Pointer of C language (2) wild pointer, what is wild pointer, and the disadvantages of wild pointer
- MySQL startup error 1607 unexpected process termination
- Multi-Modal Knowledge Graph Construction and Application: A Survey
- 9. Pointer of C language (3) classic program, exchange the value of two numbers for deep analysis, (easy to understand), are formal parameters and arguments a variable?
- Durham High Lord (classic DP)
- Product manager interview | innovation and background of the fifth generation verification code
- 跨区域网络的通信学习静态路由
猜你喜欢
随机推荐
Scene thread allocation in MMO real-time combat games
有哪个老哥知道flinksql 日志很大要怎么解决吗
Raspberry pie CM4 -- using metartc3.0 to integrate ffmpeg to realize webrtc push-pull streaming
Raspberrypico analytic PWM
WFST decoding process
Does any elder brother know how to solve the huge flinksql log
HSETNX KEY_NAME FIELD VALUE 用法
NEIL: Extracting Visual Knowledge from Web Data
Raspberry pie 4B ffmpeg RTMP streaming
[C language] string reverse order implementation (recursion and iteration)
Simple example of C language 1
robobrowser的简单使用
[C language] Fibonacci sequence [recursion and iteration]
Item exception handling in SSM
MySQL startup error 1607 unexpected process termination
Two methods to judge the size end
Longest Palindromic Substring
[C language] print pattern summary
跨区域网络的通信学习静态路由
Circular linked list OJ question
![[C language] guessing numbers game](/img/ac/81a82404618487861b67e35f18d13f.png)








