程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python automated test 23] interface automated test practice III_ Dynamic parameterization and data forgery

編輯:Python

List of articles

  • One 、 Preface
  • Two 、 Dynamic parameterization
  • 3、 ... and 、 Data forgery

One 、 Preface

This article will mainly explain the dynamic parametric form of use case design in interface testing , In addition, there is a portal for a series of articles below , It's still being updated , Interested partners can also go to check , Don't talk much , Let's have a look ~

Series articles :
Series articles 1:【Python automated testing 1】 meet Python The beauty of the
Series articles 2:【Python automated testing 2】Python Installation configuration and PyCharm Basic use
Series articles 3:【Python automated testing 3】 First knowledge of data types and basic syntax
Series articles 4:【Python automated testing 4】 Summary of string knowledge
Series articles 5:【Python automated testing 5】 List and tuple knowledge summary
Series articles 6:【Python automated testing 6】 Dictionary and collective knowledge summary
Series articles 7:【Python automated testing 7】 Data operator knowledge collection
Series articles 8:【Python automated testing 8】 Explanation of process control statement
Series articles 9:【Python automated testing 9】 Function knowledge collection
Series articles 10:【Python automated testing 10】 File basic operation
Series articles 11:【Python automated testing 11】 modular 、 Package and path knowledge collection
Series articles 12:【Python automated testing 12】 Knowledge collection of exception handling mechanism
Series articles 13:【Python automated testing 13】 class 、 object 、 Collection of attribute and method knowledge
Series articles 14:【Python automated testing 14】Python Basic and advanced exercises of automatic test
Series articles 15:【Python automated testing 15】unittest The core concept and function of test framework
Series articles 16:【Python automated testing 16】 Test case data separation
Series articles 17:【Python automated testing 17】openpyxl Secondary packaging and data driven
Series articles 18:【Python automated testing 18】 Configuration file analysis and practical application
Series articles 19:【Python automated testing 19】 Log system logging Explain
Series articles 20:【Python automated testing 20】 Construction of interface automation test framework model
Series articles 21:【Python automated testing 21】 Interface automation test practice 1 _ Interface concept 、 Project introduction and test process Q & A
Series articles 22:【Python automated testing 22】 Interface automation test practice II _ Interface framework modification and use case optimization

Two 、 Dynamic parameterization

for instance , When we design a test case of registered interface , There will be an obvious bottleneck , The mobile phone number is not registered , The registration can be completed successfully when the automated test case is executed for the first time , But the second time you execute the test case again, you will find , Cell phone number already exists , Then the second execution of the test case will fail , And in the face of that , There are several solutions :

""" Solutions : 1、 Open it manually every time excel After deletion, re-enter a new mobile phone number 2、 Query the mobile phone number in the database , If the phone number already exists , Then delete the mobile phone number in the database 3、 The last number on the existing mobile phone number +1 4、 Randomly generate a mobile phone number Thought analysis : 1、 Although the first way can solve , But it needs to be replaced manually every time , Not easy to maintain 2、 Although you can query and delete the mobile phone number , But often the real project will not easily delete the database , And a registered mobile phone number will be associated with the table , And in most cases, the test does not have permission to delete 3、 This is also a solution , We can be in the 11 Digit up , Every time +1, The disadvantage is that you will eventually encounter numbers that may conflict with other mobile phone numbers and fail to execute , But the efficiency is much higher than 1 and 2 4、 The last way is dynamic parameterization , Mark the data that needs to be replaced , When the loop traverses the tag, replace the tag with a randomly generated number , So that you can successfully pass... When executing the use case """

Dynamic parameterization is to solve this kind of problem , Let the mobile phone number change continuously and randomly , So that every execution will not lead to the failure of the use case due to the repetition of the mobile phone number ( Using dynamic parameterization may still be random to registered numbers , But the probability is very low ), And the random generation of a mobile phone number requires Data forgery .

3、 ... and 、 Data forgery

In the field of automated testing, data forgery is not something that undermines system security , Instead, we hope to automatically generate the data of test cases and the data conforms to certain rules , E.g. mobile number , Email, etc . Data forgery security can be used in the automatic registration module , It can also be used for login or other text input boxes to detect certain input rules, etc .

stay Python Data forgery database in – faker library , adopt pip Install it first and then :

import faker
# initialization faker object , Specify that the generation rule area is China 
fk = faker.Faker(locale="zh_CN")
result = fk.phone_number()
print(f" cell-phone number :{
result}")
# Randomly generate an address 
company = fk.company()
print(f" Address :{
company}")
# Randomly generate a company 
address = fk.address()
print(f" company :{
address}")
# Randomly generate a city 
city = fk.city()
print(f" City :{
city}")

In addition to the standard forgery library that can provide forgery , We can also use the way we want to make a rule :

def generate_new_phone():
phone = "1" + random.choice(["3", "5", "7", "8", "9"])
for i in range(9):
num = random.randint(0, 9)
phone += str(num)
return phone
print(f" Functional phone number :{
generate_new_phone()}")

Simple package forgery library function , If you are writing data forgery code in the framework , Then you can put it in common in , from common In order to generate some data by importing :

import faker
def generate_new_phone():
fk = faker.Faker(locale="zh_CN")
result = fk.phone_number()
return result
# Modified fragment code ( If you have questions about fragment code , Please refer to the preceding article ):
@ddt
class TestLogin(unittest.TestCase):
@list_data(data)
def test_login_success(self, case_data):
json_data = case_data["json"]
if "#new_phone#" in json_data:
new_phone = data_forgery.generate_new_phone()
json_data = json_data.replace("#new_phone", new_phone)


All right. ~ The above is all the content shared in this article , Have you learned ? I hope I can help you !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved