Python development and Interface Test learning notes, python learning notes
This is the learning notes I have accumulated as a programmer. It is easy to write and suitable for beginners who want to learn Python development and interface testing.
I. Develop a voting system
1. Refer to the documentation on the official website to create a voting system.
Https://docs.djangoproject.com/en/1.11/intro/tutorial01/
Before creating a voting system, make sure that the Django you have installed is consistent with the official website documentation.
================================
Win 10
Python 3.6.2
Django 1.11.3
Pycharm Compiler
================================
2. Create a project PollsWeb and a Polls Application
Open the Polls/setting. py file and add:
Polls is the application we just created, and bootstrap3 is the front-end framework we downloaded separately.
Final directory structure:
3. Create a model
Generally, web development first designs the database. After the database is designed, more than half of the projects are completed. This shows the importance of the database.
Open Polls/models. py and write it as follows:
Execute Database Table Generation and Synchronization
4. admin Management
Django provides powerful background management, which is essential for web applications. For example, how to add questions and question options to the current voting system? Adding a database directly is obviously troublesome, inconvenient, and insecure. Therefore, the management background can do this.
Open the Polls/admin. py file and write the following content:
The current script is used to manage the model (database table) in the admin background and run the web container:
First, create a super administrator user,
After creating a Super User, you can run our container:
Log on to our backend: 127.0.0.1: 8000/admin
Enter the login name and password we just created,
Click Add to add a question:
Click save to save
5. Write a view
View plays a leading role in connecting front-end pages and back-end databases. Query the content in the database table and display it on the page.
Compile the Polls/views. py file:
6. Configure the URL
A url is a request configuration file. It is determined by the function that forwards the request to the page.
First configure Polls/urls. py. This file needs to be created by yourself. Create a urls. py file under Polls:
After writing, edit the total urls. Open the PollWeb/urls. py file:
7. Create a template
The template is the previous page used to display data on the web page.
Create a Polls/templates/polls/directory, and create the index.html#detail.html and result.html files in the directory.
Index.html
Result.html
Detail.html
8. system function display
Http: // 127.0.0.1: 8000/polls/now we can try to visit our page:
2. Develop the voting system interface
Although the function of the voting system has been developed, we have not developed a special interface. In the current voting system, when we call a get or post request, the system returns the entire page and returns the test together with the page.
2.1 modify the voting system interface
For example, when we want to call all the problematic interfaces (test_get.py)
Run the program and get the following results:
The special interface should return data instead of the whole page. The data format is generally array, Dictionary, or Json
Therefore, you need to modify the visual layer (../Polls/views. py) to provide interfaces and simply return data.
We will first perform interface transformation on the first two. In fact, there are still many incomplete transformations here. For example, if the question_id parameter accepted is null for all option interfaces in a single problem, a parameter error should be prompted, if no problem is found, you should be prompted that the query result is null. If the data type is not a number, you should be prompted that the type is incorrect.
Run the test_get.py file again:
This is the json data. However, the return value is Unicode encoded for Chinese characters. Here is a tip to convert it into Chinese.
Http: // 127.0.0.1: 8000/polls/open in Google browser, we will see the above returned values
Click F12 on the keyboard to open the console. You can see Chinese Characters Based on the clicks circled above.
2.2. Compile the interface document
Writing interface documents is also a very important part, because the interfaces we write need to be called by others, so how do others know whether our interfaces are called using get or post? What are the parameters? Please refer to the interface documentation.
1. Get all questions
Url |
Http: // MAID: 8000/polls/ |
Request type |
Get |
Required Parameter |
None |
Return format |
Json |
Returned results |
{1: "What is the National Day", 2: "What is the Mid-Autumn Festival", 3: "What is the summer vacation", 4: "What is the start of school "} |
Error Type |
None |
2. Get all options for a single question
Url |
Http: // MAID: 8000/polls/ |
Request type |
Get |
Required Parameter |
Question_id |
Return format |
Json |
Returned results |
{1: "At Home", 2: "Work internship", 3: "surfing the Internet "} |
Error Type |
None |
This is probably the way the interface document is written. For this document, we will be able to do interface testing later.
3. for writing an interface test, we will involve two technologies. One is the unittest unit testing framework and request library.
The interface code is completed in this way. Finally, let's take a look at the returned results:
By now, our system development and interface testing have been basically completed. This is also the note I got from the tutorial with the insect.