This article mainly explains Python What is a configuration file , How to use configuration files , What are the supported configuration files , In addition, there is a portal for a series of articles below , It's still being updated , Interested partners can also go to check , Don't talk much , Let's have a look ~
Series articles :
Series articles 1:【Python automated testing 1】 meet Python The beauty of the
Series articles 2:【Python automated testing 2】Python Installation configuration and PyCharm Basic use
Series articles 3:【Python automated testing 3】 First knowledge of data types and basic syntax
Series articles 4:【Python automated testing 4】 Summary of string knowledge
Series articles 5:【Python automated testing 5】 List and tuple knowledge summary
Series articles 6:【Python automated testing 6】 Dictionary and collective knowledge summary
Series articles 7:【Python automated testing 7】 Data operator knowledge collection
Series articles 8:【Python automated testing 8】 Explanation of process control statement
Series articles 9:【Python automated testing 9】 Function knowledge collection
Series articles 10:【Python automated testing 10】 File basic operation
Series articles 11:【Python automated testing 11】 modular 、 Package and path knowledge collection
Series articles 12:【Python automated testing 12】 Knowledge collection of exception handling mechanism
Series articles 13:【Python automated testing 13】 class 、 object 、 Collection of attribute and method knowledge
Series articles 14:【Python automated testing 14】Python Basic and advanced exercises of automatic test
Series articles 15:【Python automated testing 15】unittest The core concept and function of test framework
Series articles 16:【Python automated testing 16】 Test case data separation
Series articles 17:【Python automated testing 17】openpyxl Secondary packaging and data driven
A configuration file is a file used to configure the parameters and initialization settings of a computer program , Without these configurations, the program may not run or affect the operation ( Running speed 、 Convenience, etc ), The advantage of using a configuration file is , Some contents and environment runtime only need to modify the parameter contents of the configuration file , Without having to look in the code and modify , Improve convenience 、 Improve maintainability .
There are four main forms of configuration :
The first is YAML、JSON、XML、TOML、INI、Linux In the system .bashrc A class , It is mainly used in the field of software testing , In the field of software testing , Most companies use the latest YAML Form as a configuration file , For example, database address 、 Storage of use case data and other contents , And a few companies still use the old INI Configuration form , You also need to understand and master .
The second is excel Form of form , stay excel There will be fixed... In the table title Represents the meaning of each field column , There are multiple columns , This is used to configure , Mostly used in the game field , Widely used in the game industry excel Form of form , It's already a normal state .
The third is py file ,py File for a pure Python It is very convenient for the project , It doesn't need to read data , Just import , The bad thing is , No, YAML This kind of flexible ,YAML Configuration files, whether they are Python、Java Other languages , All supported , And the data type supports many , and py Configuration file for , Can only be used for python, There are certain limitations .
The fourth is txt Text format , Identify by reading txt Text content , Generally speaking, it is a simple tool made by test engineers , It is provided to testers at the business level for use , To reduce the YAML This configuration is difficult to understand , It also avoids opening excel It's slow , With lightweight txt It's a good choice to replace .
YAML As the most common configuration file format , We have to master , Basic YAML The format reference is as follows :
# host For address ,port Is the port ,database For database address , We can go through yaml Store this data as a configuration file
# yaml The document is similar to Python Dictionary , On the left is the key , On the right is the value , The colon must be followed by a space
# key You usually don't need quotation marks , Values are usually strings, and quotation marks are recommended
host: "www.baidu.com"
port: 8899
database: "127.10.10.1"
YAML Nesting is also supported , Advanced usage , You can use its own nesting or dictionary form :
host: "www.baidu.com"
port: 8899
# This form of expression is nested
database:
url: "http://1314.520.sf"
port: 3306
name: " Cute little black house "
# It can also be modified into the form of a dictionary
database1: {
"url": "http://137.15.37.84", "Port": 8959, "name": " Cute white house "}
In addition to the dictionary , We can also use a list to store a set of data , There are also special list storage methods :
# List storage
user: ["mxt", "CSDN", "test"]
# Another form of list storage
user1:
- "mxt1"
- "CSDN1"
- "test1"
ini Format is not the mainstream configuration format at present , Most new projects adopt YAML、Excel And so on , And a few companies still use the old version ini Profile format ,ini There are too many limitations , We learn to understand the corresponding content when we encounter this kind of configuration file 、 Able to read 、 understand , There is no need to study deeply , But you also need to understand .
A basic ini The format configuration is as follows ,ini The value of the configuration file in format is only string , Even if you enter an Arabic numeral ,ini The configuration file will also treat it as a string ,key
and value
The separator and YAML Different , The equal sign is used to split
# This is a ini Configuration format for
# The code block statement does not ini Options , So the choice is YAML file , When you copy the following to your ini The document time code segment will have special color identification and highlight
[default] # The default format
host=https://www.baidu.com
port=5656
[db] # Database related usage db
host=https://kingkou.blog.csdn.net/
port=3306
nmae= Meng Xiaotian
[excel] # Table related use excel
name=cases.xlsx
We use YAML Data storage , I must hope to use these data , Then you need to read the data , stay Python In the middle of YAML Data reading can use pyyaml
, Need to go through first pip
Installation , Read it after installation .
Read YAML The data is followed by Dictionaries The way to present :
import yaml # Please note that , When importing here, it is not pyyaml, It's import yaml
with open("Demo.yaml", mode='r', encoding="utf-8") as my_yaml:
data = yaml.safe_load(my_yaml)
print(data)
YAML Support reading ,INI It also supports , When we're going to INI When reading data, you need to import a Python Built in modules , Data can be read after import :
import configparser # Import to read ini Profile data
parser = configparser.ConfigParser() # Initialize the parser
parser.read("config.ini", encoding="utf-8") # Get the data through the parser object
data = parser.get("db", "name") # Two parameters need to be filled in ,section and option,section On behalf of the group ,option For options
print(data)
All right. ~ The above is all the content shared in this article , Have you learned ? I hope I can help you !