自动化测试框架seldom

seldom 介绍

seldom 是基于 unittest 的全功能自动化测试框架;针对自动化测试达到开箱即用。

seldom特点

  • 支持测试类型(web/app/api)
  • 丰富的断言
  • 生成随机测试数据
  • 用例依赖
  • 用例分类标签
  • 支持发送(邮件、钉钉、飞书、企微)消息等
  • 日志打印
  • 缓存cache
  • 命令行工具
  • 强大的数据驱动(JSON/YAML/CSV/EXCEL)
  • HTML/XML报告
  • 失败重跑&截图
  • 数据库操作(MySQL/sqlite3/Mongodb)
  • 支持平台化

 官方文档:介绍 | seldom文档

seldom和pytest对比

seldom作者把seldom比作电脑,而把pytest比作CPU。

 

安装

pip install seldom

脚手架创建项目 

> seldom -P mypro

 创建测试用例

# test_sample.py
import seldom



class SampleTest(seldom.TestCase):

    def test_case(self):
        """a simple test case """
        self.open("http://www.itest.info")
        self.assertInUrl("itest.info")



if __name__ == '__main__':
    seldom.main()
基本规范:

创建测试类YouTest并继承seldom.TestCase类。
创建测试方法test_case, 必须以test开头。

seldom不需要初始化webdriver,也不需要定义浏览器。 只需要open方法访问url。

查看open方法的源码可以看到是seldom里定义了driver,调用了Chrome浏览器。

(这里我觉得不应该封装这么深,应该用户定义driver,再用driver去调用方法)

运行方式

  • main() 方法:在.py 文件中使用seldom.main() 方法。用python命令行执行。 python+测试文件
  • seldom 命令:通过sedom 命令指定要运行的目录&文件&类&方法。
> cd mypro/  # 进入项目根目录
> seldom -p test_dir  # 运行目录
> seldom -p test_dir/test_sample.py  # 运行文件
> seldom -m test_dir.test_sample       # 运行文件
> seldom -m test_dir.test_sample.SampleTest # 运行 SampleTest 测试类
> seldom -m test_dir.test_sample.SampleTest.test_case # 运行 test_case 测试方法

main参数

 confrun.py 配置文件

confrun.py 用于配置运行环境。 配置函数与 seldom.main() 的参数一致。

 失败重跑

设置rerun=3,在命令行运行python可以看到执行了3次,直接使用unittest执行看不出来执行了3次。

seldom.main(rerun=3)

 

 多线程运行

import seldom
from seldom.utils import threads

#这里有问题,没有打开edge浏览器
class MyTest(seldom.TestCase):

    def test_baidu(self):
        self.open("https://www.baidu.com")
        print("使用chrome运行")
        self.sleep(3)

    def test_bing(self):
        self.open("https://www.bing.com")
        print("使用edge运行")
        self.sleep(4)


@threads(2)  # !!!核心!!!! 设置线程数
def run_case(case: str, browser: str):
    """
    根据传入的case执行用例
    """
    seldom.main(case=case, browser=browser, debug=True)


if __name__ == "__main__":
    # 将两条用例拆分,分别用不同的浏览器执行
    cases = {
        "test_thread_case.MyTest.test_baidu": "chrome",
        "test_thread_case.MyTest.test_bing": "edge"
    }

    for key, value in cases.items():
        run_case(key, value)

start/end

类似setup,teardown

start()/end()  

start_class()/end_class()

class TestCase(seldom.TestCase):

    def start(self):
        print("一条测试用例开始")

    def end(self):
        print("一条测试结果")

    def test_search_seldom(self):
        self.open("https://www.baidu.com")
        self.type_enter(id_="kw", text="seldom")

    def test_search_poium(self):
        self.open("https://www.baidu.com")
        self.type_enter(id_="kw", text="poium")

 跳过测试

可以跳过测试方法或者测试类

装饰器

  • skip: 无条件地跳过一个测试。
  • skip_if: 如果条件为真,则跳过测试。
  • skip_unless: 跳过一个测试,除非条件为真。
  • expected_failure: 预期测试用例会失败。
class TestCase(seldom.TestCase):

    def start(self):
        print("一条测试用例开始")

    def end(self):
        print("一条测试结果")

    def test_search_seldom(self):
        assert 1==1
    @seldom.skip()
    def test_search_poium(self):
        assert 1==2
if __name__ == '__main__':
    seldom.main(debug=True)

 

一条通过,一条忽略。

重复执行

 @rerun(100)
    def test_search_seldom(self):
        self.open("https://www.baidu.com")
        self.type_enter(id_="kw", text="seldom")

 随机测试数据

class YouTest(seldom.TestCase):

    def test_case(self):
        """a simple test case """
        word = testdata.get_word()
        print(word)

没有使用faker库,而是seldom自己处理的方法实现的。 

用例依赖

这个比较方法,对于有依赖的用例可以不用手动定义顺序了。

import seldom
from seldom import depend


class TestDepend(seldom.TestCase):
    @depend("test_002")
    def test_003(self):
        print("test_003")
    @depend("test_001")
    def test_002(self):
        print("test_002")

    def test_001(self):
        print("test_001")



if __name__ == '__main__':
    seldom.main(debug=True)

 用例分类

设置为黑名单的标签不执行,白名单的标签执行。

from seldom import label


class MyTest(seldom.TestCase):

    @label("base")
    def test_label_base(self):
        self.assertEqual(1+1, 2)

    @label("slow")
    def test_label_slow(self):
        self.assertEqual(1, 2)

    def test_no_label(self):
        self.assertEqual(2+3, 5)


if __name__ == '__main__':
    # seldom.main(debug=True, whitelist=["base"])  # whitelist
    seldom.main(debug=True, blacklist=["slow"])    # blacklist

seldom API

Seldom API | seldom文档 (seldomqa.github.io)

Page Object

> pip install poium
import seldom
from poium import Page, Element


class BaiduPage(Page):
    """baidu page"""
    search_input = Element(id_="kw")
    search_button = Element(id_="su")


class BaiduTest(seldom.TestCase):
    """Baidu search test case"""

    def test_case(self):
        """
        A simple test
        """
        page = BaiduPage(self.driver, print_log=True)
        page.open("https://www.baidu.com")
        page.search_input.send_keys("seldom")
        page.search_button.click()
        self.assertTitle("seldom_百度搜索")


if __name__ == '__main__':
    seldom.main(browser="chrome")

发送钉钉

import seldom
from seldom import DingTalk

# ...

if __name__ == '__main__':
    seldom.main()
    ding = DingTalk(
        access_token="690900b5ce6d5d10bb1218b8e64a4e2b55f96a6d116aaf50",
        key="xxxx",
        app_secret="xxxxx",
        at_mobiles=[13700000000, 13800000000],
        is_at_all=False,
    )
    ding.sender()

 日志

from seldom.logging import log

log.trace("this is trace info.")
log.info("this is info.")
log.error("this error info.")
log.debug("this debug info.")
log.success("this success info.")
log.warning("this warning info.")

缓存

登录token,很多条用例都会用到登录token,那么就可以借助缓存来暂存登录token,从而减少重复动作

cache 本质上是通过json文件来临时记录数据,没有失效时间。你需要在适当的位置做清除操作。例如,整个用例开始时清除。

数据驱动

  • 支持使用csv、json、yaml文件进行数据驱动
  • 支持使用装饰器class_data()、@data()
  • 支持使用接口数据@api_data()
  • 支持使用DDT

数据驱动 | seldom文档

web项目

GitHub - SeldomQA/seldom-web-testing

seldom 提供了一组针对Web页面的断言方法。 

poiumopen in new window 是Page objects设计模式最佳实践。

API项目

基于seldom的接口自动化项目:

GitHub - SeldomQA/seldom-api-testing: api automation test project based on seldom framework

seldom真正的优势在断言、日志和报告 

  • 支持assertJSON()
  • 支持assertPath()
  • 支持assertSchema

seldom集成了GenSONopen in new window ,可以帮你自动生成Json schema

数据库操作

seldom 支持sqlite3、MySQL、SQL Server、MongoDB数据库操作

测试报告

使用python运行文件,会生成测试报告。如果不想每次运行都生成HTML报告,可以打开debug模式。

seldom.main(debug=True)

seldom 默认生成HTML测试报告,在运行测试文件下自动创建reports目录。

定义测试报告

seldom.main(report="./report.html",
            title="百度测试用例",
            tester="tester",
            description="测试环境:windows 10/ chrome")

测试平台

GitHub - SeldomQA/seldom-platform: Based on the seldom test platform

猜你喜欢

转载自blog.csdn.net/seanyang_/article/details/131178314