美文网首页测试框架学习
Pytest测试框架,了解下~

Pytest测试框架,了解下~

作者: aimilyzhen | 来源:发表于2019-08-10 22:11 被阅读0次


1. pytest简介

pytest是一款以python为开发语言的测试框架,具有以下优点:

  • 文档丰富,简单,易上手;
  • 支持参数化,可以细粒度地控制要测试的测试用例;
  • pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)等;
  • 很好的和CI工具结合;

2. pytest安装

2.1 Python3安装

python3-install.jpg

2.2 pytest安装

1. python3安装好后,使用pip命令安装pytest:
pip install pytest
2. 输入如下命令,查看pytest是否安装成功:
pytest --version

pytest-version.png

3. pytest测试case编写规则

  • 测试类以Test开头;
  • 测试文件以test_开头或_test结尾;
  • 测试函数以test_开头;

4. 一个例子

4.1 实现

# -*- coding:utf-8 -*-
import pytest
@pytest.mark.pytestto
class TestPytestDemo(object):
    def setup_class(self):
        pass
    @pytest.mark.asserttest
    def test_assert(self):
        assert 3 < 4, "3期望是小于4"
    @pytest.mark.strtest
    def test_str(self):
        b = "hello"
        assert "h" in b, "字符h期望在单词hello中出现"

4.2 pytest用例运行

4.2.1 运行整个testcase文件,比如测试文件名为:test_pytest_demo_ok.py。

pytest test_pytest_demo_ok.py
pytest-case-run.jpg

4.2.2 通过mark运行case

# 通过mark后的关键字制定用例运行,上个图中看到运行log中有很多警告,
# 如果不要看警告可通过参数 ----disable-pytest-warnings
pytest -m asserttest --disable-pytest-warnings
mark-run.jpg

4.3 运行结果以html格式报告输出

** 4.3.1 Pytest有个用于生成html测试结果报告的插件:pytest-html,可直接使用pip命令安装。**

pip install pytest-html

4.3.2 在执行用例时,需要加上参数:--html,如:

pytest -m pytestto --disable-pytest-warnings --html=report.html
html-report.jpg

4.3.3 指定报告的存放路径,需使用如下格式,比如,指定到一个log的文件夹里:

pytest --html=./log/report.html 
show-html-report.jpg

相关文章

网友评论

    本文标题:Pytest测试框架,了解下~

    本文链接:https://www.haomeiwen.com/subject/kvhejctx.html