如何搭建自动化测试框架?8年测试老司机浅谈一下!

48 阅读2分钟
	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 

目录组织示例


![](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/1d0e00e01daf4296b03e637ee9c1cbe5~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3MzIxMjA3NDIwNDUy:q75.awebp?rk3s=f64ab15b&x-expires=1771745445&x-signature=aYxmDswHICnODyU6DcOl2W7IFe0%3D)


### 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



现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。 如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受 可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛 分享他们的经验,还会分享很多直播讲座和技术沙龙 可以免费学习!划重点!开源的!!! qq群号:822269834【暗号:csdn999】


![](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/14f6cec0dba74a65ba8b70f87167c251~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3MzIxMjA3NDIwNDUy:q75.awebp?rk3s=f64ab15b&x-expires=1771745445&x-signature=a3uOGVvhB8g2UgB6pn3oTO5Eltw%3D)


### 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),