preface
I summarized a conventional interview question about interface testing. Now interface automation testing is used more and is favored by many companies. So what capabilities do you need to have to do interface automation testing?
That is, during the interview, what questions will the interviewer take? Do you know if you have really done interface automation test? In general, the following questions are frequently asked:
1. What is the difference between JSON and dictionary- Investigation on basic data types
2. Where do you put the test data- Separation of data and script
3. Parameterization data driven mode
4. The request parameters of the next interface depend on the return data parameter association of the previous interface
5. How does the login dependent interface handle the management of - token and session
6. How to handle the return of mock simulation data by relying on the interface of the third party
7. How to deal with irreversible operations, such as deleting an order, and how to test and create data
8. How to clean up the garbage data generated by the interface - Data Cleaning
9. How to measure all the statuses of an order, such as: unprocessed, processing, processing failure, processing success - create data and change the order status of the database
10. How does Python connect to the database?
11. Others are related to running reports, code management (git), running strategies and continuous integration of jenkins
1. What is the difference between JSON and dict?
Now, the rotten street of automation training is the last few frameworks that everyone can say. If you ask questions about the framework during the interview, the job seeker only needs a bottle of sprite from 82 years, which will make you doubt your life!
So in order to know more clearly whether you are staying on the surface or have a solid foundation, the interviewer won't ask about the frame. Basically, if you ask the basis of several data types, you will know whether they are available or not.
So what's the difference between json and dictionary? Beginners don't even know the basic data types of python, so they directly roll out the framework. Some people may be confused after learning for several months and think json is a dictionary. This is definitely wrong.
First of all, the basic data types in python include int, str, float, list, bool, tuple, dict and set. There is no json data type in python.
JSON (JavaScript object notation) is a lightweight data exchange format. Based on a subset of ECMAScript (js specification formulated by the European Computer Association), it uses a text format completely independent of the programming language to store and represent data. The concise and clear hierarchy makes JSON an ideal data exchange language. It is easy for people to read and write, but also easy for machine analysis and generation, and effectively improves the network transmission efficiency.
Because your code is written in python (it may also be php,java,c, ruby and other languages), but the back-end interface is written in java (it may also be other languages), the data types of different languages are different (for example, the language data types of China are different from those of the United States. Generally speaking, a sheep and a cow in China and a /an in the United States), Therefore, the data you submit cannot be recognized by other development languages. Therefore, it is necessary to standardize the transmitted data (the transmitted data is a string). Everyone follows a specification and transmits it in a standard format, so there is json, an internationally standardized data type.
json is essentially a string, just a string in the format of key: value pairs
import json # a is dictionary dict a = {"a": 1, "b": 2, "c": True} # b is json b = '{"a": 1, "b": 2, "c": true}' print(type(a)) print(json.dumps(a)) # a to json
Operation results
<class 'dict'> {"a": 1, "b": 2, "c": true} <class 'str'> {'a': 1, 'b': 2, 'c': True}
2. Where do you put the test data?
How to put the test data is a favorite question for interviewers. It seems that different people have different opinions. There is no standard answer. Some people say to put excel and others say to put it py script, or put ini configuration file,
There are also json, yaml files, txt files, and even databases. There are all kinds of places for 100 automation partners.
Here is a summary of how to put the test data?
First of all, there are many kinds of test data, including login account data, registered account data, interface parameters, mailbox configuration data, etc., so this question can not be generalized. Or dig a hole for yourself.
The following two taboos cannot be answered:
- The test data cannot be written in the code. This is a matter of principle and a taboo in writing the code (if you answer to write in the code, you may go back and wait for the notice)
- Put the test data into the At the beginning of py, this is actually very convenient. For a small amount of fixed data, it can be put. However, during the interview, you must not say so. The interviewer likes to pretend to force
Test data storage summary:
1. For account and password, this kind of global parameter can be extracted separately from the command line parameter and written into the configuration file (such as ini)
2. For some one-time consumption data, such as registration, different numbers can be generated by random functions
3. There are multiple groups of test parameters for an interface, which can be parameterized. The data can be put into yaml, text, JSON and excel
4. The data that can be used repeatedly, such as the data that needs to be created in various states of the order, can be put into the database, initialized each time, and cleaned up after use
5. For some parameters of mailbox configuration, you can use ini configuration file
6. For all independent interface projects, data-driven mode can be used to manage the tested interface data with excel/csv
7. For a small amount of static data, such as the test data of an interface, i.e. 2-3 groups, it can be written to the beginning of the py script and will not change for ten or eight years
In short, different test data can be managed with different files
3. What is data driven and how to parameterize it?
The concept of parameterization and data-driven must be known. The idea of parameterization is that after the code use case is written, there is no need to change the code, just maintain the test data, and generate multiple use cases according to different test data
The unittest framework is used in python
import unittest import ddt # test data datas = [ {"user": "admin", "psw": "123", "result": "true"}, {"user": "admin1", "psw": "1234", "result": "true"}, {"user": "admin2", "psw": "1234", "result": "true"}, {"user": "admin3", "psw": "1234", "result": "true"}, {"user": "admin4", "psw": "1234", "result": "true"}, {"user": "admin5", "psw": "1234", "result": "true"}, {"user": "admin6", "psw": "1234", "result": "true"}, {"user": "admin7", "psw": "1234", "result": "true"}, {"user": "admin8", "psw": "1234", "result": "true"}, {"user": "admin9", "psw": "1234", "result": "true"}, {"user": "admin10", "psw": "1234", "result": "true"}, {"user": "admin11", "psw": "1234", "result": "true"}] @ddt.ddt class Test(unittest.TestCase): @ddt.data(*datas) def test_(self, d): """Shanghai-long:{0}""" print("Test data:%s" % d) if __name__ == "__main__": unittest.main()
The unittest framework also has a paramunittest that can also be implemented
import unittest import paramunittest import time # python3.6 # Author: Shanghai - youyou @paramunittest.parametrized( {"user": "admin", "psw": "123", "result": "true"}, {"user": "admin1", "psw": "1234", "result": "true"}, {"user": "admin2", "psw": "1234", "result": "true"}, {"user": "admin3", "psw": "1234", "result": "true"}, {"user": "admin4", "psw": "1234", "result": "true"}, {"user": "admin5", "psw": "1234", "result": "true"}, {"user": "admin6", "psw": "1234", "result": "true"}, {"user": "admin7", "psw": "1234", "result": "true"}, {"user": "admin8", "psw": "1234", "result": "true"}, {"user": "admin9", "psw": "1234", "result": "true"}, {"user": "admin10", "psw": "1234", "result": "true"}, {"user": "admin11", "psw": "1234", "result": "true"}, ) class TestDemo(unittest.TestCase): def setParameters(self, user, psw, result): '''Notice here, user, psw, result The three parameters correspond to the dictionary defined above one by one''' self.user = user self.user = psw self.result = result def testcase(self): print("Start executing use cases:--------------") time.sleep(0.5) print("Enter user name:%s" % self.user) print("Input password:%s" % self.user) print("Expected results:%s " % self.result) time.sleep(0.5) self.assertTrue(self.result == "true") if __name__ == "__main__": unittest.main(verbosity=2)
If the pytest framework is used, parameterization can also be realized
# content of test_canshu1.py # coding:utf-8 import pytest @pytest.mark.parametrize("test_input,expected", [ ("3+5", 8), ("2+4", 6), ("6 * 9", 42), ]) def test_eval(test_input, expected): assert eval(test_input) == expected if __name__ == "__main__": pytest.main(["-s", "test_canshu1.py"])
pytest also has a more powerful function, which can obtain all combinations of multiple parametric parameters and stack parametric decorators
import pytest @pytest.mark.parametrize("x", [0, 1]) @pytest.mark.parametrize("y", [2, 3]) def test_foo(x, y): print("Test data combination: x->%s, y->%s" % (x, y)) if __name__ == "__main__": pytest.main(["-s", "test_canshu1.py"])
4. The request parameters of the next interface depend on the return data of the previous interface
This is very easy. Different interfaces are encapsulated into different functions or methods. The required data is return ed and accepted with an intermediate variable a. the later interface can be passed to a
Refer to this article [python interface automation 26- parameter association and jsessionid (the data returned from the previous interface is used as the request parameter of the next interface)]
5. How to handle the login dependent interface
If the login interface depends on a token, you can log in first and then save the token into a yaml, json, or ini configuration file. All subsequent requests to get this data can be used globally
Refer to a previously shared article python interface automation 24 - the interface project with token is designed using unittest framework
If it is a cookie parameter, it can be automatically associated with session
s=requests.session()
Later requests can automatically associate cookies with s.get() and s.post()
6. How to handle interfaces that depend on third parties
You need to build a mock service yourself to simulate the data returned by the interface. Refer to [python note 25 mock server moco]( https://www.cnblogs.com/yoyoketang/p/9348552.html)
Moco is an open source framework that can be downloaded from GitHub https://github.com/dreamhead/moco
moco service construction needs to be mastered by yourself. The interview will ask you how to build it, how to simulate the returned data, what format is used and how to request it
7. How to handle irreversible operations, such as deleting an order, and how to test the interface
This question tests the ability to create data and the request data of the interface. Many of them need to rely on the previous state
For example, workflow flows to different people with different states and operation permissions. When testing, each state should be measured, so you need to be able to create data yourself.
Normally, manually test and create data, and directly change the field status in the database. So is automation. You can connect the database with python to create data, and do the operation of adding, deleting, modifying and querying
Pre operation of test cases and data preparation for setUp
Post operation, tearDown data cleaning
8. How to clean up the garbage data generated by the interface
As above, to create data and clean up data, you need to connect the database with python to add, delete, modify and query
Pre operation of test cases and data preparation for setUp
Post operation, tearDown data cleaning
9. How to measure all the statuses of an order, such as unprocessed, processing, processing failure and processing success
Like the above, it also examines the status of creating data and modifying data
10. How does Python connect to the database?
This is a detailed investigation of how you use python to connect the database, and it is best to write code on site (some written questions are python to connect the database)
Which module do you use and what type of data are queried? How to delete data? How to add data? How to modify data?
PyMySQL is in Python 3 A library used to connect to MySQL server in version x, and mysqldb is used in Python 2.
Refer to the tutorial for details http://www.runoob.com/python3/python3-mysql.html
#!/usr/bin/python3 # Query all data with salary field greater than 1000 in EMPLOYEE table: import pymysql # Open database connection db = pymysql.connect("localhost","testuser","test123","TESTDB" ) # Use the cursor() method to get the operation cursor cursor = db.cursor() # SQL query statement sql = "SELECT * FROM EMPLOYEE \ WHERE INCOME > %s" % (1000) try: # Execute SQL statement cursor.execute(sql) # Get a list of all records results = cursor.fetchall() for row in results: fname = row[0] lname = row[1] age = row[2] sex = row[3] income = row[4] # Print results print ("fname=%s,lname=%s,age=%s,sex=%s,income=%s" % \ (fname, lname, age, sex, income )) except: print ("Error: unable to fetch data") # Close database connection db.close()
Others are related to running reports, code management (git), running strategies and continuous integration of jenkins. This automation is the same, but we will talk about jenkins continuous integration later
Communication QQ group: 779429633