微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

001、pytest环境、用例设计原则、Pycharm设置pytest运行器

 

1、pytest环境:

  安装指令:pip install pytest

  查看版本:pip show pytest

C:\Users\27806>pip show pytest
Name: pytest
Version: 5.4.3
Summary: pytest: simple powerful testing with Python
Home-page: https://docs.pytest.org/en/latest/
Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
Author-email: None
License: MIT license
Location: c:\skyworkspace\worktools\python\python38\lib\site-packages
Requires: atomicwrites, more-itertools, wcwidth, py, pluggy, colorama, attrs, packaging
required-by: pytest-Metadata, pytest-html, httprunner, allure-pytest

 

2、用例设计原则

  文件名,以  test_* .py 开头  或 *_test.py 结束  (必须加下划线,否则识别不到)

  以 Test 开头的类  (可以加下划线 Test_ )

  以 test_ 开头的函数方法  (可以不加下划线)

  所有的包package都必须要有 __init__.py 文件

 

test_aa.py 的代码如下:
class Testaa():
    def testaa(self):
        a = 'hello'
        b = 'world'
        print('=============test_aa==============')
        assert a in 'hello world'

test_bb.py 代码如下:

def test_bb():
    a = 'hello'
    b = 'world'
    print('=============test_bb==============')
    assert a in 'hello world'

test_cc.py 代码如下:

def test_cc():
    a = 'hello'
    b = 'world'
    print('=============test_cc==============')
    assert a in 'hello world'

执行结果如下:

 

3、Pycharm设置pytest运行器

  在 pycharm中 以pytest方式运行,需要改该工程设置认的运行器:file->Setting->Tools->Python Integrated Tools->项目名称->Default test runner->选择pytest;

        建议:在开始写代码之前就设置pytest运行器,如果在写了代码之后再设置pytest运行器的话,设置pytest运行器之前写的类,在类名后右击,不会弹出 run pytest for .....

  鼠标放在 class类名后面,右击。  

  备注:设置pytest运行器之前写的类,在类名后右击,不会弹出 Run pytest for .....

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐