Through the above code, we found that we are adjusting the login interface, but the parameters are not the same. Writing this way will easily cause too much code, so we can use parameterization to write test cases, and parameterization we can Use the parameterized module. For more parameterized usage methods, you can visit the parameterized official website. The optimized code is as follows (we updated the user, username admin, password 123456):
# -*- coding: utf -8 -*-
import requests
import unittest
from parameterized import parameterized
class BlogTest(unittest.TestCase):
< br /> def setUp(self):
self.url = "http://127.0.0.1:8000/"
self.username ='admin'
self.password = '123456 '
def tearDown(self):
pass
@parameterized.expand([
("01", "", "admin111111", 0,'username or password null'),
("02", "admin", "", 0, "username or password null"),
("03", "", "" , 0, "username or password null"),
("04", "admin1", "admin111111", 0, "username or password error"),
("05", "admin" , "admin1111112", 0, "username or password error"),
("06", "admin1", "admin1111112", 0, "u sername or password error"),
("07", "admin", "123456", 1, "login success"),
])
def test_login(self, testcase_number, username , password, status, msg):
url = self.url +'login'
payload = {'username': username,'password': password}
self.result = requests. post(url, payload).json()
# print(self.result)
self.assertEqual(self.result['status'], status)
self.assertEqual(self. result['message'], msg)
def test_request_type_error(self):
url = self.url +'login'
payload = {'username': self.username ,'password': self.password}
self.result = requests.get(url, payload).json()
# print(self.result)
self.assertEqual(self. result['status'], 0)
self.assertEqual(self.result['message'],'request type error')
@parameterized.expand([
("01", "", "Homesickness", "Yu Guangzhong", "When I was young, homesickness was a small stamp. I am here and my mother is there. When I grow up, homesickness is a narrow ferry ticket. I am here and the bride is at the other end.', 0,
'id or title or author or content null'),
("02 ", "1", "", "Yu Guangzhong",'When I was young, homesickness was a small stamp. I am here and my mother is on the other. When I grow up, homesickness is a narrow ticket, I On this end, the bride is on the other end', 0,
'id or title or author or content null'),
("03", "2", "Homesickness", "",'When I was young , Homesickness is a small stamp. I am here and my mother is at the other end. When I grow up, homesickness is a narrow boat ticket. I am here and the bride is at the other end', 0,
'id or title or author or content null'),
("04", "3", "homesickness", "余光中",'', 0,'id or title or author or content null') ,
("05", "5", "Homesickness", "Yu Guangzhong",'When I was young, homesickness was a small stamp. I am here and my mother is on the other. When I grow up, homesickness is A narrow ticket, I am here, and the bride is at the other end',
1, "add article success"),
("06", "1", "homesickness", " Yu Guangzhong",'When I was young, homesickness was a small stamp. I was on this end and my mother was on the other. When I grew up, homesickness was a narrow boat ticket. I was on this end and the bride was on the other end.
0, "article id already exists"),
("07", "3", "The title of nostalgia is too long and more than 10 characters", "余光中",'When I was young, homesickness was a small Small stamps, I am here and my mother is at the other end. When I grow up, homesickness is a narrow ticket. I am here and the bride is at the other end.',
0,'The blog title is too long '),
("08", "4", "Homesickness", "Yu Guangzhong's author's name is too long and greater than 10 characters",'When I was young, homesickness was a small stamp. I'm here, mother At the other end. When I grow up, homesickness is a narrow ticket. I am here and the bride is at the other end.',
0 ,'Author name is too long'),
])
def test_create_blog(self, testcase_number, b_id, title, author, content, status, msg):
""" Use parameterized testing to create a blog"""
url = self.url +'add_article'
payload = {'id': b_id,'title': title,'author': author,'content' : content}
self.result = requests.post(url, payload).json()
# print(self.result)
# self.assertEqual(self.result['status' ], status)
self.assertEqual(self.result['message'], msg)
@parameterized.expand([
("01", "10", 1,'delete article success'),
("02", "11", 0,'id not exist'),
("03", "aa", 0,'parameter type error '),
])
def test_delete_blog(self, testcase_number, b_id, status, msg):
"""Use parameterized tests to delete blogs and create a blog first"" "
create_blog_url = self.url +'add_article'
payload = {'id': '10','title':'homesickness',
'author': ' Yu Guangzhong','content':'When I was young, I was homesick It's a small stamp. I'm on this end and my mother is on the other end. When I grow up, homesickness is a narrow ferry ticket. I am here and the bride is at the other end.'}
requests.post(create_blog_url, payload).json()
url = self.url +'delete_article'
payload = {'id': b_id}
self.result = requests.post(url, payload).json()
# print(self. result)
self.assertEqual(self.result['status'], status)
self.assertEqual(self.result['message'], msg)
@parameterized .expand([
("01", "", "Homesickness", "Yu Guangzhong",'When I was young, homesickness was a small stamp. I am here and my mother is there. When I grow up, Homesickness is a narrow boat ticket, I am here, the bride is at the other end', 0,
'id or title or author or content null'),
("02", "12 ", "", "Yu Guangzhong",'When I was young, homesickness was a small stamp. I was on this end and my mother was on the other. When I grew up, homesickness was a narrow boat ticket. I was here. The bride is over there', 0,
'id or title or author or content null'),
("03", "12", "Homesickness", "",'When I was young, homesickness was one A small stamp. I am here and my mother is at the other end. When I grow up, homesickness is a narrow ticket. I am here and the bride is at the other end.', 0,
'id or title or author or content null'),
("04", "12", "Homesickness", "余光中",'', 0,'id or title or author or content null'),
("05", "12", "township "Sorrow", "Yu Guangzhong", "When I was young, homesickness was a small stamp. I am here and my mother is there. When I grow up, homesickness is a narrow ferry ticket. I am here and the bride is at the other end. 1',
1, "modify article success"),
("06", "12 ", "The title of homesickness is too long and more than 10 characters", "Yu Guangzhong", "When I was young, homesickness was a small stamp. I was on this side and my mother was on the other. When I grew up, my homesickness was a narrow one. Ferry ticket, I am here, the bride is at the other end',
0,'blog title is too long'),
("07", "12", "homesickness", "Yu Guangzhong, the author's name is too long Longer than 10 characters",'When I was young, homesickness was a small stamp. I was here and my mother was on the other. When I grew up, homesickness was a narrow boat ticket. I was here and the bride was here. The other end',
0,'The author's name is too long'),
("08", "aa", "Homesickness", "Yu Guangzhong",'When I was young, homesickness was a small stamp , I am here and my mother is at the other end. When I grow up, homesickness is a narrow ferry ticket. I am here and the bride is at the other end.', 0,
'Parameter type error'),< br /> ("09", "13", "Homesickness", "Yu Guangzhong",'When I was young, homesickness was a small stamp. I am here and my mother is there. When I grow up, my homesickness is a stamp Narrow boat ticket, I am here, the bride is at the other end', 0,'article not exist'),
])
def test_modify_blog(self, testcase_number, b_id, title, author, content , status, msg):
"""Use parameterized testing to modify blog first create a blog"""
create_blog_url = self.url +'add_article'
payload = { 'id': '12','title':'homesickness',
'author':'Yu Guangzhong','content':'When I was young, homesickness was a small stamp. I am here, mother Over there. When I grow up, homesickness is a narrow ferry ticket. I am here and the bride is at the other end.'}
requests.post(create_blog_url, payload).json()
url = self.url +'modify_article'
payload = {'id': b_id,'title': title,'author': author,'content': content}
self.result = requests.post (url, payload).json()
# print(self.result)
self.assertEqual(self.result['status'], status)
self.assertEqual(self.result ['message'], msg)
def test_query_blog(self):
"""Query blog, without conditions"""
create_blog_url = self. url +'add_article'
payload = {'id': '14','title':'mushroom picking',
'author':'testDog','content':'mushroom picking Girl, carrying a big basket'}
requests.post(create_blog_url, payload).json()
url = self.url +'get_article'
payload2 = {}
self.result = requests.post(url, payload2).json()
# print(self.result)
self.assertEqual(self.result['status'], 1) < br /> self.assertEqual(self.result['message'],'success')
def test_query_title_blog(self):
"""Query blog by title"""
title = "Jumping leather rope"
create_blog_url = self.url +'add_article'
payload = {'id': '15','title': title,
'author' :'testDog','content':'Little girl picking mushrooms, like to jump leather rope'}
self.result = requests.post(create_blog_url, payload).json()
# print(self .result)
url = self.url +'get_article'
payload = {"title": title}
self.result = requests.post(url, payload).json()< br /> # print(self.result)
self.assertEqual(self.result['status'], 1)
self.assertEqual(self.result['message'],'success')
self.assertEqual(self.result['data'][0]['title'], title)
if __name__ =='__main__':
unittest.main()
Welcome to follow the WeChat official account: Software Test Wang. Software testing exchange group: 809111560