当前位置:网站首页>Practice of interface automation from 0.1

Practice of interface automation from 0.1

2021-08-10 08:13:58 Test Mengmeng


Since childhood, the teacher will tell us : A bad pen is better than a good head , It's your own thing to remember, learn and write , On a whim, I've been rolling out the front-end code for more than three months , Finally, a training organized by the unit made me feel again about automated testing , Then simply write down some practices of interface automation , I hope I can give some help to students who want to learn automation but can't start , Step on less holes , I'll start with simple examples and code , Don't worry about not understanding , Because it's too difficult and complex, I won't . Before we get down to business , Let's start with a few words ( On the blackboard ), When it comes to automation , What many people think of or have heard of is all kinds of recording tools to assist in testing , image selemium ah ,Appnium Ah, these , Although these are also a kind of Automation , But I always don't advocate using these tools , Because you can use these tools with only long hands and no brain , The code needs to be written by yourself, not automatically generated by tools , It's like you've been lazy ordering takeout every day , After a long time, the knife won't work , Anyway, I was busy cooking for more than two hours half a month ago, and I couldn't talk about it myself .

 picture

Previous work

All right. , Let's get to the point , Now there are many frameworks for interface testing , Most companies use Postman、JMeter、RobotFramework etc. , However, this article is intended to guide novice students to understand the principle of interface testing , So it's still from 0 Start writing , Deepen the understanding . People are from 0 Start , I'll write from 0.1 Start , It's not pretending X La , this 0.1 It requires you to know a little scripting language ,Python、Ruby、JAVA Anything can be , Here's a novice friendly Python Write... As an example (Ruby I will not ). install Python You can go to the official website https://www.python.org/downloads/ download , The version I installed myself is 3.5.2( Now? Py2.X No more updates , skilled 2.7 I'd better change my friend to Py3 Well ), Very simple installation , Keep going. The next step is , Remember to tick... In the last step Add Python 3.5 to PATH Again install, Otherwise, there may be an error ‘python’ Not an internal or external command , Nor are they runnable programs or batch files , This is because... Is not checked as mentioned above , Then we need to manually handle python.exe The path where is added to Path in , No, you can refer to jdk install . After the installation is successful, click on the command line python, If the message is as follows , Description installation successful :

 picture

About IDE Nothing to say. , Direct next Pycharm Well ,http://www.jetbrains.com/pycharm/download/#section=windows So you can set breakpoints as much as you like 、 Step by step . Students who are not good at English can have a Chinese package .

At this time, students without code foundation need to find a book to start learning fundamentals , Go through the basics and get familiar with unit testing 、 With some knowledge such as serialization and network request, you can do the following .

 picture

Simulate network requests

Interface testing is generally based on HTTP and HTTPS Network request for , I understand HTTP The protocol and HTML file , We actually understand one Web The essence of application is :

a. Browser sends a HTTP request ;

b. The server receives the request , Generate a HTML file ;

c. Server handle HTML Document as HTTP Responsive Body Send it to the browser ;

d. Browser received HTTP Respond to , from HTTP Body Take out HTML Document and display .

The underlying code is implemented by special server software , We use it Python Focus on generating HTML file . Because we don't want to touch TCP Connect 、HTTP Original request and response format , therefore , Need a unified interface , Let's concentrate on Python To write Web Business .WSGI The interface meets our needs , The definition is very simple , It only requires Web The developer implements a function , I can respond HTTP request . but WSGI The interface is still too low-level ,WEB Development focuses on efficiency , Is there a more abstract way to reduce the work of developers ? The answer, of course, is yes , How else ” Life is too short for me Python“ Well . What we use more now is httplib Module and requests modular , I'd better use a little more concise requests Library as an example .

Import requests
Use pip To extend third-party libraries , open CMD Input pip install requests, The prompt is shown in the figure below, indicating that the import is successful :

 picture

After the import is successful, you can start the journey of simulating network requests . Not much nonsense. , The code is shown :

simulation GET Request mode :

 picture

simulation POST Request mode :

 picture

requests Library documents for everyone :

http://docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests

A simple interface automation test case

The above two simulated request methods are only basic knowledge , Now let's start the real interface automation verification , A little assertion knowledge is needed here , Forget the students to make up for it first .

Before you start, you need to understand the business requirements you want to test , Ask your product manager for a project prototype , Unfortunately, our company doesn't have a product manager , Then ask the project manager for a requirements document , At least understand the important logic inside . Second, you need to understand the code logic , Business logic is not equal to code logic , Look at the code written by others , It's also fun to do a unit test . It's said that Ali's test drive review Developed code , I don't know if it's true. -. Then there is the standard interface document , Standardized interface documents can make interface testing get twice the result with half the effort . Here is what I use excel A simple interface document written by hand ( The work computer is not connected , Document screenshots cannot be transmitted , I'm so angry )

 picture

It is estimated that everyone is in a hurry , Finally, it's time to start interface testing , Check the interface document , See if the return result is the same as expected , Here we need unittest The module is quite skilled , Test the returned results through assertions , Let's take a look at the test template I wrote myself :

 picture

Executing a single use case can be performed in the form of test entry code , There are many use cases, and another method is recommended : On the command line, pass the parameter -m unittest myapi_test Direct operation , More automation friendly .

Start interface automation testing

Why are two lines of dividing lines used above , Because the above is still the foundation , I wrote so much just to let everyone learn , Step by step , Understand thoroughly , The interface automation will begin next .

 picture

Think about the projects at hand , In contrast, select the simplest business logic to write an example of interface automation . In the temporary route module of the pre flight system just completed, there is a logic that airlines add routes , Extract a simple and clear business requirement as follows

 picture

According to the business needs , The functions to be processed are defined as follows :

 picture

Before starting Automation , We also need to understand the database knowledge , At least we should understand the relationship between tables .

The code I write below may be a little more than the code for the test template above , But the general idea is the same , The comparison notes should be understandable . It should be noted that the actual work is manual route content , Here I wrote file-like Import form of , Remember, I told you to learn first JSON Knowledge of serialization , It's here , Transmission requires JSON Deserialization of

 picture

The above is only the design and implementation of an interface , But in practice, a test can run more than one interface , Hundreds and thousands of case Yes, there are. . So write a use case that automatically runs the interface , This can reflect Automation

 picture

The above code can make the automation case run automatically and output the test report .with Students who don't know much about sentences can find and learn by themselves , It is mainly to ensure that the file reading and writing can be closed correctly in case of error .

The above is only the implementation of interface automation , Generally speaking, it is still rough , In practice, these case It needs to be optimized . For example, add a configuration file ( Configuration database 、url、 User information 、 Status codes, etc. )、 Add initialization profile case、 Add configuration database connection and get users cookie Documents, etc . It's hard to digest if you say too much at one time , Optimize case Write again when you are free at the weekend .

If the code gives you a headache , Please finish reading . Remove code , The interface automation framework is nothing more than the following structure , It's not too cumbersome , Does it feel much better than generating worthless code with automated tools . I think it's hard because your code base is not very strong , First, consolidate the foundation , Then look at the structure I wrote again , You will feel so easy.( The coder doesn't need to , As for the code, it's not very professional , Don't care about the details )

 picture

I'll talk again sometime next week Jenkins The practice of continuous integration automation , These are all made by myself , No one's directing , If there is a great God, please clap the brick , There are colleagues who can make progress together . The writing style is average , Poor typesetting , Who doesn't have the ideal of being a writer .

 picture

PS: The unit is disconnected today , The packing drawing doesn't bring , The remote can't reach the working computer , Take a screenshot of the code to go home and start all over again , Interface documents are for your own use excel Handwritten , Patchy look ( Silent tears , There should be applause here ~)


Last : Welcome everyone to follow the official account :【 Sad spicy bar 】, Take one 300 page pdf Document Python Summary of the core knowledge of automation test engineer !

Most of the information in the official account is the knowledge point that the interviewer will ask when interviewed , It also includes a lot of common knowledge in the testing industry , It includes basic knowledge 、Linux necessary 、Shell、 The principles of the Internet 、Mysql database 、 Special topic of bag capturing tools 、 Interface testing tool 、 Test advanced -Python Programming 、Web automated testing 、APP automated testing 、 Interface automation testing 、 Testing advanced continuous integration 、 Test architecture development test framework 、 Performance testing 、 Safety test, etc. .

If you have a lot of confusion in the test , Then the software testing technology exchange group I created will be a useful community for you to contact good teachers and friends , Peers may bring you some practical help and breakthrough . Group :902061117 You also want to know how your peers are getting rich !

If it helps you a little , Everyone 「 give the thumbs-up 」 Is the biggest driving force of Xiaobian's creation , See you in the next article !

Good article recommends :

Package as 1 Years working experience as a test engineer , My advice to him before the interview is as follows

interviewer : Work for three years , And a preliminary test ? I'm afraid the title of your software test engineer should be enclosed in double quotation marks …

“ One year in the job , The automated software test hired by high salary was discouraged .”

The man who leaves work on time , Promoted before me …

Summary of automated testing
 Insert picture description here

版权声明
本文为[Test Mengmeng]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/08/20210810081223765O.html

随机推荐