The built-in function of pytest makes you no longer worry about automatic test data storage.

Time:2022-1-14

In the run automation test, some test data will be used in the test process, including temporary test data and commonly used data. The frequently used data can be stored in the form of Excel or yaml file. What about the temporary file?

Today, let’s introduce a built-in function in pytest, which can easily help us solve this problem.

TMPDIR is a built-in function in pytest. This function means to create a temporary file directory before the test starts running and delete it after the test ends.

This temporary file directory can be used to store our test data.

usage method

Write TMPDIR directly after the test case, which needs to be created directly when creating temporary files or temporary folders.

import pytest

def test_01(tmpdir):
#Create a test directory
a = tmpdir. MKDIR ('test directory ')
#Create a test file
aa = a.join('123.txt')
#Write a line of text in this test file
aa. Write ('This is a test file ')
Print ('test directory address:% s'% a)
#Read what is written in the file
txt = aa.read()
print(txt)
Assert TXT = = 'this is a test file'

if __name__ == '__main__':
pytest.main(['-vs'])

Read the contents of the file through pytest, and we can find that the temporary file has been created. In order to ensure quiet, I went to the bottom of this folder and looked at it. It does have content.

在这里插入图片描述
在这里插入图片描述

Note: Although the temporary file was not deleted at that time, pytest will automatically overwrite the temporary file after we call the temporary file again.

Actual combat drill

The above has introduced how to use the built-in functions of TMPDIR. Here, you can request two interfaces and demonstrate how to use them in the project.

  • Interface 1: fill in a random mobile phone number and query the city address through the mobile phone number home interface.
  • Interface 2: query the local weather forecast through the city address of interface 1 above.

The data generated here: the query results of the mobile phone number and the local weather forecast are written into the temporary file through TMPDIR.

import pytest
import requests
def phone():
'' query mobile phone home '' '
url = 'http://apis.juhe.cn/mobile/get'
data = {
'key':"7d2b9b14adfe392c88b9c431297be543",
'phone':"15641111111"
}
r = requests.post(url,data=data)
return r.json()
def tianqi(city):
'' query weather forecast '' '
url = 'http://apis.juhe.cn/simpleWeather/query'
data = {
'city':city,
'key':'331eab8f3481f37868378fcdc76cb7cd'
}
r = requests.post(url,data=data)
return r.json()
def test_01(tmpdir):
'' 'test case 1' ''
#Call the mobile phone home interface
ph = phone()
#Create a temporary file
a = tmpdir.join('123.txt')
#Write interface return content in temporary file
a.write(ph)
#Get city name
city = eval(a.read())['result']['city']
Print ('address of city is:% s'% city ')
#Execute query weather interface
result = tianqi(city)
#Write the weather interface information to the file
a.write(result)
print(result)
if __name__ == '__main__':
pytest.main(['-vs'])

It can be seen from the execution interface that both interfaces have been successfully executed.

请添加图片描述
By viewing the temporary file directory, the temporary file content is successfully created.
请添加图片描述

summary

Quiet simply introduces the built-in function TMPDIR of pytest through a small example. Of course, there is more than one method to create temporary files here. Quiet just uses this example to let you simply understand TMPDIR.

For specific and detailed operations, please refer to the official documents. I hope this library can help you. Thank you for reading.

Other columns

Comprehensive application of reptile + regularization + selenium Technology

Five minutes to test your suitability for this job with a monthly salary of 25K

Recommended Today

Springboot notes (III) redis

Redis database **Soul torture: * * didn’t you learn MySQL and save data? Why do you learn another database? Previously, we learned MySQL database, which is a traditional relational database. We can use Mysql to better manage and organize our data. Although in small web applications, only a MySQL + mybatis built-in cache system can […]