编程技术分享平台

网站首页 > 技术教程 正文

如何组织自动化测试工程的目录结构?

xnh888 2024-11-17 17:26:03 技术教程 38 ℃ 0 评论

公司使用python+selenium+pytest来做UI自动化测试,我经历了从0开始搭建自动化工程、开发测试报告web服务、编写测试用例,经过了一段时间的磨合,感觉已经比较顺手了,这里梳理下我采用的自动化工程目录组织结构。

整体目录结构及说明

整个目录结构如下,其中第一级目录和文件有:

  • conf目录:主要用来存放项目运行环境、执行环境相关的配置参数
  • logic目录:与项目有关的关键字、业务逻辑封装放到此目录
  • testsuite目录:测试用例在此目录编写,pytest默认约定test开头的文件和方法为测试用例,不满足条件的不会被执行,内部建议按照特性建立文件夹对测试用例进行分类
  • utils目录:把与业务无关的实用程序放到此目录,比如自己写的辅助方法
  • .gitignore文件:git提交忽略文件配置文件
  • conftest.py文件:pytest的fixture方法可以写在这里,测试用例使用其中的fixture不需要使用import显示引入,不过目前我没有使用这个文件
  • pytest.ini文件:可以针对pytest进行一些配置,如日志格式和级别等,后面会展示其配置内容
  • requirements.txt文件:把需要安装的python第三方库写入此文件,需要使用该工程时只需要执行pip install -r requirements.txt就可以一次性安装全部依赖
  • runall.py文件:执行全部用例入口,主要用来给jenkins等CI/CD工具拉起自动化任务使用,后面会展示其内容
  • README.md文件:自动化工程说明文档
project_name
	conf
		settings.py
	logic
		__init__.py
		logic.py
		mysql.py
	testsuite
		feature01
			test_feature01.py
		feature02
			test_feature02.py
	utils
		utils.py
	.gitignore
	conftest.py
	pytest.ini
	requirements.txt
	runall.py
	README.md 

目录组织示例

pytest.ini配置

下面的配置主要是针对日志格式和级别进行了设置,log_cli开头的配置是针对控制台输出设置的,log_file开头的是针对输出到日志文件来设置的,会生成run.log日志文件,在./log目录下,如果log目录不存在会自动创建。

[pytest]
log_cli = 1
log_cli_level = INFO
log_cli_date_format = %Y-%m-%d-%H-%M-%S
log_cli_format = %(asctime)s - %(filename)s - %(module)s - %(funcName)s - %(lineno)d - %(levelname)s - %(message)s
log_file = ./log/run.log
log_file_level = INFO
log_file_date_format = %Y-%m-%d-%H-%M-%S
log_file_format = %(asctime)s - %(filename)s - %(module)s - %(funcName)s - %(lineno)d - %(levelname)s - %(message)s

requirements.txt

列举依赖的python第三方库,可以指定版本,也可以不指定,不指定的情况下默认安装最新版本。文件示例内容如下,其中selenium、pytest、pytest-html、pytest-xdist是必须的,其他需要根据自己的项目情况来定。

xlrd >= 1.2.0
openpyxl >= 3.0.4
requests >= 2.24.0
psutil >= 5.7.2
pymysql >= 0.10.0
selenium
pytest
pytest-html
pytest-repeat
elasticsearch
pyyaml
jsonpath
python-dateutil
paramiko
pytest-xdist
xlwt
pandas

runall.py

runall.py文件主要是给jenkins等CI/CD工具拉起自动化任务使用的,它的主要作用就是执行pytest.main方法来使用pytest框架来收集并执行测试用例,最终会生成html和xml两份报告。

import os
import time
import pytest

from conf.settings import *

def new_report_dir():
    return time.strftime("%Y-%m-%d")

def new_report_name(project="demo", file_type="html"):
    """file_type: 文件后缀名,默认为html
    """
    now = time.strftime("%Y-%m-%d_%H-%M-%S")
    report_name = "{2}_{0}.{1}".format(now, file_type, project)
    return report_name

if __name__ == "__main__":
    report_base_dir = r".\report"
    report_html = os.path.join(report_base_dir, new_report_dir(), new_report_name())
    report_xml = os.path.join(report_base_dir, new_report_dir(), new_report_name("demo", "xml"))
    pytest.main(["--html={0}".format(report_html), 
                "--junit-xml={0}".format(report_xml), 
                "-n {0}".format(CPU_NUM),
                "--dist=loadfile"])

但是,pytest默认生成的html报告并不友好,如果还想把历史数据做下统计分析就需要使用生成的xml报告了。比如通过flask框架可以快速定制开发出web服务,通过持续采集解析xml报告写入数据库作为数据源,不仅能更好地展现单次测试报告的数据,而且还能分析历史数据的统计曲线。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表