Sitelet https://web.archive.org/web/20230322152739/https://github.com/oslo254804746/AutoMationTest
Skip to content
This repository has been archived by the owner on Feb 18, 2023. It is now read-only.

oslo254804746/AutoMationTest

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

简介

基于pytest二次封装调整的测试框架,封装了一些钩子以及常用工具,可以满足简单的测试需求,如果需要大型的测试,需要额外封装功能

使用简介

安装requirements.txt, 由于接口测试通用性实在不怎么样,各个公司甚至各个需求所对应的认证请求逻辑都不一样,因此这里就不封装了,封装了一个UI和数据库的,接口的自行进行封装

1 配置

配置都使用config模块中的配置,根据启动pytest时指定环境进行切换配置

如果env 不存在,则使用configs.json中的默认配置

pytest ./ --env dev # 使用config-dev.json中的配置
pytest ./ --env prod # 使用configs-prod.json中的配置

所有跟用例相关的配置可以通过configs获取

from config.globalVars import configs
configs.get_config('api', 'host')

通过这样实现一个动态配置

覆盖json文件中的配置通过--config 设置

pytest ./ --env dev --config api.host=www.baidu.com

--config 设置的配置会覆盖掉当前配置,如上述会先切换到dev环境的配置,在进行覆盖

pytest.ini中设置了mark严格,使用@pytest.mark.TestCase外的自定义标签会报错

2 输出

2.1 不指定环境运行输出:

(venv)  root@root-263  ~/PycharmProjects/AutoMationTest   master ±✚  pytest -s testcase/test_demos.py::test_read_yml
运行环境为: 测试
收到外部配置为: []
================================================================================================= test session starts ==================================================================================================
platform darwin -- Python 3.9.1, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: /Users/wangbaofeng/PycharmProjects/AutoMationTest, configfile: pytest.ini
collected 1 item                                                                                                                                                                                                       

testcase/test_demos.py [INFO] [2021-06-26 16:54:40,092] [test_demos.py:18] : {'登录': 'xpath=/html/body/div[1]/div[1]/div[4]/a', '用户名登录': 'xpath=/html/body/div[4]/div[2]/div[2]/div/div/div/div/div/div[3]/p[2]', xpath=/html/body/div[4]/div[2]/div[2]/div/div/div/div/div/div[1]/form/p[5]/input[2]', '密码': 'xpath=/html/body/div[4]/div[2]/div[2]/div/div/div/div/div/div[1]/form/p[6]/input[2]', '确认登录': 'xpath=/html/body/div[42]/div[2]/div/div/div/div/div/div[1]/form/p[9]/input'}
.测试详情


通过用例:
test_read_yml


================================================================================================== 1 passed in 0.01s ===================================================================================================

2.2 执行环境/配置输出

(venv)  ✘ root@rootdeMacBookPro-263  ~/PycharmProjects/AutoMationTest   master ±✚  pytest -s testcase/test_demos.py::test_read_yml --env prod --config api.host=www.baidu.com
运行环境为: prod
收到外部配置为: ['api.host=www.baidu.com']
================================================================================================= test session starts ==================================================================================================
platform darwin -- Python 3.9.1, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: /Users/wangbaofeng/PycharmProjects/AutoMationTest, configfile: pytest.ini
collected 1 item                                                                                                                                                                                                       

testcase/test_demos.py [INFO] [2021-06-26 16:58:51,020] [test_demos.py:18] : {'登录': 'xpath=/html/body/div[1]/div[1]/div[4]/a', '用户名登录': 'xpath=/html/body/div[4]/div[2]/div[2]/div/div/div/div/div/div[3]/p[2]', xpath=/html/body/div[4]/div[2]/div[2]/div/div/div/div/div/div[1]/form/p[5]/input[2]', '密码': 'xpath=/html/body/div[4]/div[2]/div[2]/div/div/div/div/div/div[1]/form/p[6]/input[2]', '确认登录': 'xpath=/html/body/div[42]/div[2]/div/div/div/div/div/div[1]/form/p[9]/input'}
.测试详情


通过用例:
test_read_yml


================================================================================================== 1 passed in 0.01s ===================================================================================================
(venv)  wangbaofeng@wangbaofengdeMacBookPro-263  ~/PycharmProjects/AutoMationTest   master ±✚  

3 运行结果

如果环境不是test, 会在运行结束后根目录下生成result.json,内容如下

{"cases": [{"start_time": 1624698133000, "end_time": 1624698133000, "level": "1", "result": "Success", "case_name": "", "function_name": "test_read_yml", "case_module": "", "duration": 0}], "duration": 0, "startTime": 1624698133000, "endTime": 1624698133000, "result": "Success", "passRent": 1.0}

Cases 字段对应为数组类型,包含了本地运行任务中所有用例运行信息

startTIme和endTime 对应为任务运行时间以及结束时间

duration为任务耗时

result为测试结果,本次运行所有用例都通过为Success, 包含失败则为Failed

passRent为通过率

cases字段对应数组中,每一个Map为一个测试用例,分别包含了

Start_time/end_time 开始/结束时间

Level 用例等级,通过在@pytest.mark.TestCase注解中参数进行获取

result 对应该用例运行结果,setup,call, teardown均通过为Success,setup/teardown报错为Error, call报错为Failed(skip/xfail均为Faild)

Case_name 用例名称 同Level

Function_name 测试用例函数名,

Case_module 用例所属模块,同Level

4 提供功能

由于经历有限,只提供几个功能,在lib目录下

1 db

对python的SQLAlchemy进行的一个封装,包含了Mysql, Oracle客户端,以及Redis连接

但是本人未测试,可能有些报错,需要自行debug调试

抽象基类

class Client(ABC):

    @abstractmethod
    def select(self, *args, **kwargs):
        pass

    @abstractmethod
    def insert(self, *args, **kwargs):
        pass

    @abstractmethod
    def update(self, *args, **kwargs):
        pass

    @abstractmethod
    def delete(self, *args, **kwargs):
        pass

导入方式

from lib.db import MysqlClient, OracleClient, RedisClient

参数说明:

Mysql

def __init__(self,
             host: AnyStr,
             username: AnyStr,
             password: AnyStr,
             port: Optional[int] = 3306,
             database: Optional[AnyStr] = None,
             **kwargs,
             ):
    pass

Oracle

    def __init__(self,
                 host: AnyStr,
                 port: int,
                 database: AnyStr,
                 username: AnyStr,
                 password: AnyStr,
                 mode: Optional[AnyStr] = None,
                 **kwargs):
                 pass

Redis

    def __init__(self,
                 host: AnyStr,
                 port: Optional[int] = 6379,
                 username: Optional[AnyStr] = 'root',
                 password: Optional[AnyStr] = '',
                 db_index: Optional[int] = 0,
                 Pool: bool = False):
                 pass

2 files

提供了一个读取文件的类

使用方式

from lib.files import ReadFileData
data = ReadFileData("path/to/your/file")
data = data.data # 数据在data属性上


excel_data = ReadFileData("path/to/your/file",line=[1,3]) # 读取1,2行excel数据,与python切片对应
csv_data = ReadFileData("path/to/your/file",line=5) # 读取第五行数据

json_data = ReadFileData("path/to/your/file",key='wang') # 只读取key对应value

支持格式

支持json, yaml, xlsx, csv, 同时csv 以及 xlsx可以指定line读取,yaml,json可以指定key读取

3 http

只提供一个Ping

from lib.http import Ping
result :bool = Ping("https://www.baidu.com")

result为True或者False

4 log,os,path

基本上是对loging, os, shutil的二次封装,不再赘述

5 ui

提供一个获取驱动的类

from lib.ui import Driver
driver = Driver(browser="Chrome", headless=True)
request.cls.driver = driver.driver

只需要指定 浏览器,支持Mac OS/Linux下 firefox, Chrome, windows下 IE/firefox/chrome

5 致谢

感谢jetbrains提供的大力支持jetbrains

About

A simple Test Program based on Pytest

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages