前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pytest学习和使用2-初步使用和用例运行

pytest学习和使用2-初步使用和用例运行

原创
作者头像
虫无涯
发布2023-02-06 09:48:51
1980
发布2023-02-06 09:48:51
举报
文章被收录于专栏:全栈测试技术

1 测试脚本

  • pytest_study文件夹下创建一个test_mm.py
代码语言:python
代码运行次数:0
复制
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2021/8/27 16:51
# 文件名称:test_mm.py
# 作用:xxx
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

def m_sum(x):
    return x * (x+1)

def test_m_sum():
    assert m_sum(3) == 11
  • pytest_study文件夹下打开cmd直接输入pytest运行:
代码语言:python
代码运行次数:0
复制
(venv) F:\pytest_study>pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.7.0, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: F:\pytest_study
plugins: allure-pytest-2.8.12, cov-2.8.1, forked-1.1.3, html-2.0.1, metadata-1.8.0, ordering-0.6, xdist-1.31.0
collected 1 item

test_mm.py F                                                                                                                                                      [100%]

=============================================================================== FAILURES ===============================================================================
______________________________________________________________________________ test_m_sum ______________________________________________________________________________

    def test_m_sum():
>       assert m_sum(3) == 11
E       assert 12 == 11
E        +  where 12 = m_sum(3)

test_mm.py:13: AssertionError
======================================================================= short test summary info ========================================================================
FAILED test_mm.py::test_m_sum - assert 12 == 11
========================================================================== 1 failed in 0.07s ===========================================================================
  • 发现执行失败了,是因为12不等于11,assert是用来进行断言的。

2 脚本分析

2.1 断言使用assert

  • 从上边脚本看到断言的话使用assert即可,根据官网的说法是pytest断言基本都是用的assert

2.2 使用pytest运行用例规则

  • 文件名规则:
代码语言:python
代码运行次数:0
复制
test_*.py和*_test.py命名的函数
  • 函数名规则:
代码语言:python
代码运行次数:0
复制
以test_开头的函数
  • 类的规则
代码语言:python
代码运行次数:0
复制
test_开头的方法,不能有__init__ 方法
  • python包的规则
代码语言:python
代码运行次数:0
复制
同python一样,包需要有__init__.py文件
  • -q-quiet参数进行静默运行函数(说白了就是结果输出简单化)

3 练习下用例运行规则

  • 先在pytest_study目录下再新建一个test_case包;
  • 然后把之前写的第一个用例test_mm.py移动到这个目录下:
  • 在pytest_study下执行pytest,发现是可以执行的,说明是执行了test_case下的test_mm.py
代码语言:python
代码运行次数:0
复制
(venv) F:\pytest_study>pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.7.0, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: F:\pytest_study
plugins: allure-pytest-2.8.12, cov-2.8.1, forked-1.1.3, html-2.0.1, metadata-1.8.0, ordering-0.6, xdist-1.31.0
collected 1 item

test_case\test_mm.py F                                                                                                                                            [100%]

=============================================================================== FAILURES ===============================================================================
______________________________________________________________________________ test_m_sum ______________________________________________________________________________

    def test_m_sum():
>       assert m_sum(3) == 11
E       assert 12 == 11
E        +  where 12 = m_sum(3)

test_case\test_mm.py:13: AssertionError
======================================================================= short test summary info ========================================================================
FAILED test_case/test_mm.py::test_m_sum - assert 12 == 11
========================================================================== 1 failed in 0.13s ===========================================================================

(venv) F:\pytest_study>
  • 使用-q参数来执行下,一下子少了很多输出:
代码语言:python
代码运行次数:0
复制
(venv) F:\pytest_study>pytest -q
F                                                                                                                                                                 [100%]
=============================================================================== FAILURES ===============================================================================
______________________________________________________________________________ test_m_sum ______________________________________________________________________________

    def test_m_sum():
>       assert m_sum(3) == 11
E       assert 12 == 11
E        +  where 12 = m_sum(3)

test_case\test_mm.py:13: AssertionError
======================================================================= short test summary info ========================================================================
FAILED test_case/test_mm.py::test_m_sum - assert 12 == 11
1 failed in 0.05s

(venv) F:\pytest_study>
  • 再在test_case下新建一个名为test_a的包,并在包里复制一个test_mm1.py
  • 从执行结果看是ok的,那么以上都证明了文件名、包名都需要以test开头才能被执行;
代码语言:python
代码运行次数:0
复制
(venv) F:\pytest_study>pytest -q
FF                                                                                                                                                                [100%]
=============================================================================== FAILURES ===============================================================================
______________________________________________________________________________ test_m_sum ______________________________________________________________________________

    def test_m_sum():
>       assert m_sum(3) == 11
E       assert 12 == 11
E        +  where 12 = m_sum(3)

test_case\test_mm.py:13: AssertionError
______________________________________________________________________________ test_m_sum ______________________________________________________________________________

    def test_m_sum():
>       assert m_sum(3) == 11
E       assert 12 == 11
E        +  where 12 = m_sum(3)

test_case\test_a\test_mm1.py:13: AssertionError
======================================================================= short test summary info ========================================================================
FAILED test_case/test_mm.py::test_m_sum - assert 12 == 11
FAILED test_case/test_a/test_mm1.py::test_m_sum - assert 12 == 11
2 failed in 0.20s

(venv) F:\pytest_study>
  • 我们修改下test_mm1.py增加一个类,如下:
代码语言:python
代码运行次数:0
复制
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2021/8/27 16:51
# 文件名称:test_mm.py
# 作用:xxx
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import pytest


class TestClass:

    def test_you(self):
        y = "you"
        assert "y" in y

    def test_hai(self):
        h = "hai"
        assert "gg" not in h


if __name__ == '__main__':
    pytest.main()
  • 直接在pytest_study下执行pytest -q,如下,说明类也执行到了:
代码语言:python
代码运行次数:0
复制
(venv) F:\pytest_study>pytest -q
...                                          [100%]
3 passed in 0.15s

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 测试脚本
  • 2 脚本分析
    • 2.1 断言使用assert
      • 2.2 使用pytest运行用例规则
      • 3 练习下用例运行规则
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档