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

Python+pytest automated test function test class test method encapsulation

編輯:Python

Catalog

Preface

One 、 General rules of test case encapsulation

3、 ... and 、 Test class / Method encapsulation

Four 、 Sample code

summary

Preface

What about today , I want to talk to you python+pytest Interface automation encapsulates code , Only by encapsulating the test code , To be recognized and executed by the test framework .

For example, the request code of a single interface is as follows :

import requestsheaders = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"}url = "https://mp.toutiao.com/profile_v4/"res = requests.get(url=h_url, headers=headers)

Suppose we need to write the above code into test cases that the test framework can execute , It's obviously not enough just to write code like this , The following supplements are needed :

You need to encapsulate the code into a unit test framework (pytest or unittest) Recognizable test functions or test classes , Otherwise, it will not be recognized and executed . Need to add assertions , That is, the comparison between the result and the expectation , The unit test framework can determine whether the execution result of the use case passes , result == Expectation means passing , Otherwise failure .

python The encapsulation of functions and classes in is not described here , The purpose of this article is Let us understand how to encapsulate the test code in the interface automation test .

One 、 General rules of test case encapsulation

There are two ways to encapsulate test cases , Test functions and test classes , The general rules of encapsulation are as follows :

A test function corresponds to a test case . Multiple test methods can be defined in the test class , A test method corresponds to a test case , A test class can be seen as a set of test cases .pytest The name of the test function or test method in must be named with test start , The test class name must begin with Test start . For specific naming rules, please refer to my previous article pytest(3)- Test naming rules . For the test and verification of single interface , A single interface test case contains only one interface request , Encapsulate an interface request into a test function or test method . For the scene ( Multiple interfaces ) Test and verification of , A scenario test case needs to request multiple interfaces , Therefore, multiple interface requests need to be encapsulated in the same test function or method . Generally, it encapsulates the forward verification of an interface 、 Exception checking is encapsulated into different methods , And encapsulated in the same test class . For example, define a login test class , Correct user name 、 The password request is encapsulated into a method ( A test case ), Correct user name 、 The wrong password request is encapsulated into another method ( Another test case ). You can also encapsulate the interface use cases associated with a function point or function in the same test class . For example, the interface involved in the personal Center , Can be encapsulated in the same test class 、 Encapsulation of test functions

generally speaking , A test function corresponds to a use case . The above code is written as a test case , Examples are as follows :

  emphasize ,pytest The test function in must be named with test start , Such as test_get_home.

3、 ... and 、 Test class / Method encapsulation

A test class is equivalent to a set of test cases , Each method in the class corresponds to a test case . Take the login interface as an example , Encapsulated into a test class , Examples are as follows :

  emphasize ,pytest The name of the test class in needs to be named with Test start , Such as TestLogin, And the test class cannot have init Method . The test method in the test class must be expressed in test start , Such as test_login_normal.

Four 、 Sample code

pytest You can use the command line or use code, that is pytest.main() Execute use cases , Specific can refer to the article pytest(1)- brief introduction .

The complete example code is as follows :

import requestsimport pytestimport jsondef test_get_home(): ''' Request home page interface :return: ''' headers = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36" } url = "https://www.cnblogs.com/lfr0123/" res = requests.get(url=url, headers=headers) # Assertion , Judge the return result code Is it equal to 200, Of course, in the actual interface test, there are other fields that need to be asserted in the returned results assert res.status_code == 200class TestLogin: ''' Login interface verification ''' url = "http://127.0.0.1:5000/login" headers = {"Content-Type": "application/json;charset=utf8"} def test_login_normal(self): ''' Correct user name 、 Log in with the correct password ''' data = { "username": "AndyLiu", "password": "123456" } res = requests.post(url=self.url, json=data, headers=self.headers) # Assertion assert res.status_code == 200 assert json.loads(res.text)["token"] def test_login_error(self): ''' Correct user name 、 Wrong password login ''' data = { "username": "AndyLiu", "password": "111111" } res = requests.post(url=self.url, json=data, headers=self.headers) # Assertion assert res.status_code == 200 assert not json.loads(res.text)["token"]if __name__ == '__main__': pytest.main() summary

Test functions 、 Test class / Encapsulation of test methods , In fact, no matter what the unit test framework is , Follow the same way .

Each has its own requirements in terms of naming , such as pytest And unittest There are some differences in test naming methods in .

Think of a function or method with its own assertions as a test case , So a test class is actually a test case set containing one or more test cases , Each method in the class corresponds to a test case .

Which test methods are placed in a test class , In other words, which test cases should be included in a test case set , This can be determined according to the situation of the project itself , It can also be determined according to the tester's own ideas , The main idea is to be clear

This is about python+pytest This is the end of the article on automated test function test class test method encapsulation , More about python Please search the previous articles of SDN or continue to browse the related articles below. I hope you can support SDN more in the future !



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