Article Directory
1. Background:
At present, in the process of automatically generating test reports for interfaces, it is more popular to generate reports in combination with Allure. In most of the Allure reports generated in the process, the environment variable column is empty. This article mainly introduces how to add in Allure reports. Environment variable description
2. Preparation work:
2.1. Ideas for adding environment variables:
If you can see the environment variable parameters in the Allure report, you must add the environment variable parameters and properties files in the generated xml file (that is, the generated automated test data) to achieve the purpose of display, so we can generate a copy first
. After the .properties file is directly copied to the test data (if you don’t understand here, you can look at the main function I shared before), so as to achieve our goal
2.2, copy the file to the specified directory
import shutil
def mycopyfile(srcfile, dstpath): # 复制函数
"""
将源文件srcfile,复制并粘贴到执行路径下dstpath
"""
if not os.path.isfile(srcfile):
print("%s not exist!" % (srcfile))
else:
fpath, fname = os.path.split(srcfile) # 分离文件名和路径
if not os.path.exists(dstpath):
os.makedirs(dstpath) # 创建路径
shutil.copy(srcfile, dstpath + fname) # 复制文件
print("copy %s -> %s" % (srcfile, dstpath + fname))
2.2.1. Call method
if __name__ == "__main__":
mycopyfile(f"{
BASE_DIR}/test_datas/data_type/properties/windows/environment.properties",
f"{
BASE_DIR}/test_datas/data_type/properties/linux/")
After solving the problem of copying and moving files, it is necessary to solve the parsing of the .properties file
2.3, .properties file analysis
class Properties(object):
"""
.properties文件读取&写入操作
"""
def __init__(self, filename, encoding='utf-8'):
# filename,详细的.properties文件路径
self.filename = filename
self.fp_read = open(self.filename, 'r', encoding=encoding)
self.data = self.fp_read.readlines()
self.fp_read.close()
self.properties = {
}
for i in self.data:
if '#' in i:
continue
sp = i.split('=')
key = sp[0].replace(' ', '')
value = sp[-1].replace('\n', '').replace(' ', '')
# .properties文件值存储为键值对形式,存在相同的key值则update更新
self.properties.update({
key: value})
def get(self, key):
# 输入键值对key值,获取对应value值
return self.properties[key]
def set(self, key, value):
"""
输入键值对:key-value,相同则覆盖更新,不通则写入
"""
self.fp_write = open(self.filename, 'w', encoding='utf-8')
try:
self.properties[key] = value
except:
pass
data = ""
for k, v in self.properties.items():
data += k + " = " + v + "\n"
self.fp_write.writelines(data[:-1])
self.fp_write.close()
2.3.1. Call method:
if __name__ == '__main__':
p = Properties(f"{
BASE_DIR}/test_datas/data_type/properties/windows/environment.properties", "utf-8")
p.get('systemVersion')
p.set('env', 'sit3')
3. Add environment variables to the report
3.1. Idea:
Prepare file A, copy file A after generating pytest use case data
3.2. Execution:
pytest.main(
['-s', '-v',
'--alluredir', f'{
BASE_DIR}/report/xml/' + new_dir_name]) # 生成xml
pro_path = f'{
BASE_DIR}/report/xml/' + new_dir_name + '/'
dir_path = f"{
BASE_DIR}/test_datas/data_type/properties/windows/environment.properties"
p = Properties(dir_path, "utf-8")
p.set('environmentVersion', read_environment('env'))
mycopyfile(dir_path, pro_path)
command_word = f'allure generate {
BASE_DIR}/report/xml/{
new_dir_name} -o {
BASE_DIR}/report/html/{
new_dir_name} --clean'
os.system(command_word) # 执行cmd命令,生成html
Students who can't connect can read my previous article