Skip to content

Commit

Permalink
自动化测试初次提交
Browse files Browse the repository at this point in the history
  • Loading branch information
kali-autocore committed Aug 7, 2020
1 parent a9951b8 commit 80113e8
Show file tree
Hide file tree
Showing 249 changed files with 1,501 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/auto_test.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,59 @@
# autotest
# AUTOCORE自动化测试
框架: pytest+allure

#python版本
Ubuntu自带的Python3.6
安装pip: sudo apt install python3-pip
#安装JDK
```
sudo apt-get update
sudo apt-get install openjdk-8-jdk
sudo update-alternatives --set java /usr/lib/jvm/jdk1.8.0_version/bin/java # 设置环境变量
java -version # 显示版本信息表示安装成功
```

# 安装Allure
sudo apt-get update
sudo apt-get install allure

# python 模块安装
```
pip install pytest
pip install allure-pytest
pip install pytest-assume # assert 失败还继续进行
```

# 自动化测试目录说明
![avatar](./doc/autotest.png)
1. testcases -- 测试用例
2. allure_results -- 测试结果
3. allire_reports -- 测试报告
4. .pytest_cache -- 本次测试的执行状态
5. common -- 封装的公共方法
6. doc -- 测试文档,可存放某些详细的说明
7. logs -- 本次执行的测试日志
9. rosbag -- 测试用到的rosbag包
10. utils -- 封装的常用工具
11. venv -- 虚拟环境
12. config.py -- 测试配置
13. conftest.py -- 最顶成的测试固件
14. pytest.ini -- pytest配置
15. run.py -- 执行测试用例脚本

# 插件
1. pytest-repeat 重复执行
2. pytest-rerunfailures 失败用例再执行
3. pytest-assume 多重断言 前边的断言失败后,后边的断言还会继续执行,有助于分析那些断言失败,比原有的更高级
4. pytest-xdist 分布式执行用例,节省时间,属于进程级别的并发
5. allure-pytest 生成十分完美的测试报告,allure serve ./report/xml
Overview:总览
Categories:类别,默认是分了failed和error,凡是执行结果是其中一个的都会被归到类里面,可以通过这里快捷查看哪些用例是failed和error的
Suites:测试套件,就是所有用例的层级关系,可以根据package、module、类、方法来查找用例
Graphs:测试结果图形化,包括用例执行结果的分布图,优先级,耗时等
Timeline:可以看到测试用例精确的测试时序(执行顺序),包括执行时间
Behaviors:行为驱动,根据epic、feature、story来分组测试用例(后面会讲到)
Packages:这就是按照package、module来分组测试用例了

# 框架学习
https://www.cnblogs.com/poloyy/tag/Pytest/
https://www.cnblogs.com/luizyao/tag/pytest/
24 changes: 24 additions & 0 deletions allure_results/categories.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"name": "Ignored tests",
"matchedStatuses": ["skipped"]
},
{
"name": "Infrastructure problems",
"matchedStatuses": ["broken", "failed"],
"messageRegex": ".*bye-bye.*"
},
{
"name": "Outdated tests",
"matchedStatuses": ["broken"],
"traceRegex": ".*FileNotFoundException.*"
},
{
"name": "Product defects",
"matchedStatuses": ["failed"]
},
{
"name": "Test defects",
"matchedStatuses": ["broken"]
}
]
5 changes: 5 additions & 0 deletions allure_results/environment.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PCU-IP=192.168.1.102
PCU-USER=user
SDK-VER=1.1.1
TEST-ENV=192.168.1.33
AUTOWARE_VER=1
1 change: 1 addition & 0 deletions common/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# -*- coding:utf8 -*-
24 changes: 24 additions & 0 deletions common/command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding:utf8 -*-
"""
命令
"""
from config import PCU_IP, LOCAL_IP

# ====================== source env ======================
SOURCE_1 = 'source /opt/ros/melodic/setup.bash'
SOURCE_2 = 'source /opt/autocore/ros1_ws/setup.bash'
EXPORT_IP = 'export ROS_IP={}'.format(PCU_IP)
EXPORT_MASTER = 'export ROS_MASTER_URI=http:https://{}:11311/'.format(PCU_IP)
EXPORT_LOCAL_IP = 'export ROS_IP={}'.format(LOCAL_IP)


# ====================== local command ======================
ROS_PLAY_BAG_MAP = '{0};{1};{2};$(nohup rosbag play /home/duan/PycharmProjects/auto_test/rosbag/make_map.bag > /dev/null 2>&1 &) && sleep 1'.format(SOURCE_1, EXPORT_LOCAL_IP,
EXPORT_MASTER)

# ====================== launch command ======================
RADAR_MAPPING = '{};{};{};{}; $(nohup roslaunch radar_mapping radar_mapping.launch cellWidth:=1.0 radar_back_right_topic:=/radar408_raw2 point_map_path:=/opt/autocore/test_data/map radar_front_topic:=/radar408_raw1 keyFrameStep:=5.0 gps_topic:=/map_navsatfix cellPointNumThreshold:=3 distanceThreshold:=30.0 radar_back_left_topic:=/radar408_raw3 point_map_topic:=/radar_map cellHeight:=3 > /dev/null 2>&1 &) && sleep 3'.\
format(SOURCE_1, SOURCE_2, EXPORT_IP, EXPORT_MASTER)

RADAR_PF_LOCALIZER = '{};{};{};{}; $(roslaunch radar_pf_localizer radar_pf_localizer.launch line_x_dev_:=2.0 tf_br_yaw_:=-2.5 gps_lat_dev_:=0.5 tf_fm_y_:=0 utm_path_:=%s tf_bl_y_:=0.90 tf_br_y_:=-0.9 angle_z_dev_:=0.000017 tf_bl_x_:=-1.85 tf_br_x_:=-1.85 particles_pro_dis_:=10 map_dir_:=%s tf_fm_yaw_:=0 sigma_hit_:=0.5 gps_lon_dev_:=0.2 map_scale_:=0.5 particles_num_from_gps_:=1000 tf_bl_yaw_:=2.42 particles_num_for_pf_:=2000 tf_fm_x_:=2.85 > %s 2>&1 &) && sleep 3'.\
format(SOURCE_1, SOURCE_2, EXPORT_IP, EXPORT_MASTER)
128 changes: 128 additions & 0 deletions common/process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# -*- coding:utf8 -*-
"""
进程操作
1. 启动进程,并检测
2. 杀掉进程,并检测
"""
import os
import time
import logging
logger = logging.getLogger()


def local_start_process(command, check_key, start_time=2):
"""
本地启动进程
:param command: 启动命令
:param check_key: 检测启动命令key
:param start_time: 进程启动时间
:return:
"""
logger.info('exec local command: {}'.format(command))
os.system(command)

# 等待进程启动
logger.info('wait time: {}'.format(start_time))
time.sleep(start_time)

# 检查启动结果
cmd = 'ps -ef | grep {} | grep -v grep'.format(check_key)
logger.info('check process, check key: {}, check command: {}'.format(check_key, cmd))
ret_obj = os.popen(cmd)
ret = ret_obj.read()
logger.info('stdout: {}'.format(ret))
if check_key in ret:
return True, 'start successfully, exec command: {}'.format(command)
return False, 'start failed, exec command: {}'.format(command)


def remote_start_process(server, command, check_key, start_time=2):
"""
远程启动命令
:param server: 远程服务
:param command: 执行命令
:param check_key: 检测启动成功关键字
:param start_time: 启动时间
:return:
"""
try:
ret = server.exec_comm(command)
logger.info('stdout: {}'.format(ret.stdout))

# 等待进程启动
logger.info('wait time: {}'.format(start_time))
time.sleep(start_time)

# 检查启动结果
cmd = 'ps -ef | grep {} | grep -v grep'.format(check_key)
ret = server.exec_comm(cmd)
ret = ret.stdout
logger.info('stdout: {}'.format(ret))
if check_key in ret:
return True, 'start successfully, command: {}'.format(command)
else:
return False, 'exec failed, command: {}'.format(command)

except Exception as e:
logger.exception(e)
return False, 'raised except, command: {}'.format(command)


def remote_stop_process(server, process_name, kill_cmd='-9', stop_time=1):
"""
远程停止进程
:param server: 远程服务
:param process_name: 进程名字,必须包含在运行的进程命令中,否则不会被kill
:param kill_cmd: 停止进程方式
:param stop_time: 检测进程停止的时间
:return:
"""
try:
server.exec_comm('kill {} `ps -ef|grep "{}"|awk \'{{print $2}}\'`'.format(kill_cmd, process_name))
# 进程停止所需时间
logger.info('wait time: {}s'.format(stop_time))
time.sleep(stop_time)
# 检查是否kill 成功
ret = server.exec_comm('ps -ef| grep {} | grep -v grep'.format(process_name))
ret = ret.stdout
logger.info('stdout: {}'.format(ret))
if process_name in ret:
return False, 'stop failed, remote process : {}'.format(process_name)
return True, 'stop successfully, remote process : {}'.format(process_name)
except Exception as e:
logger.exception(e)
return False, 'raised except, stop remote process : {}'.format(process_name)


def local_stop_process(process_name, kill_cmd='-9', stop_time=1):
"""
远程本地进程
:param process_name: 进程名字,必须包含在运行的进程命令中,否则不会被kill
:param kill_cmd: 停止进程方式
:param stop_time: 检测进程停止的时间
:return:
"""
try:
stop_cmd = 'kill {} `ps -ef|grep "{}"|awk \'{{print $2}}\'`'.format(kill_cmd, process_name)
logger.info('local stop process: {}'.format(stop_cmd))
ret_obj = os.popen(stop_cmd)
ret = ret_obj.read()
logger.info('stdout: {}'.format(ret))

# 进程停止时间
logger.info('wait time: {}s'.format(stop_time))
time.sleep(stop_time)

# 检查是否kill 成功
check_cmd = 'ps -ef| grep {} | grep -v grep'.format(process_name)
logger.info('local check process: {}'.format(check_cmd))
ret_obj = os.popen(check_cmd)
ret = ret_obj.read()
logger.info('stdout: {}'.format(ret))

if process_name in ret:
return False, 'stop failed, local process : {}'.format(process_name)
return True, 'stop successfully, local process : {}'.format(process_name)
except Exception as e:
logger.exception(e)
return False, 'raised except, stop local process : {}'.format(process_name)
11 changes: 11 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding:utf8 -*-

PCU_IP = '192.168.1.102'
PCU_USER = 'user'
PCU_PWD = 'user'
LOCAL_IP = '192.168.1.33'

REMOTE_TEST_DATA = '/opt/autocore/test_data/' # PCU存放测试数据的路径
TEST_CASE_PATH = '/home/duan/PycharmProjects/auto_test/' # 测试用例所在目录

TEST_REPORT_LOG = 'data/attachments/logs/' # 测试日志在报告中的位置,相对路径
Loading

0 comments on commit 80113e8

Please sign in to comment.