pytest安装及基本使用

  在学习pytest之前一直使用的是unittest,一直听说pytest相比于unittest更加灵活,年底了终于有时间学习一下pytest了!

一、pytest的安装

pip install pytest

二、验证pytest是否安装成功

pytest --version

出现以上内容表示pytest安装成功!

三、pytest的默认约束规则

  1. 所有单元测试文件名都需要满足格式:test_*.py  或 *_test.py ;
  2. 在单元测试文件中,测试类需要以Test开头,并且不能带有init方法(注意:定义class时,需要以T开头,不然pytest是不会去运行该class的);
  3. 在单元测试类中,可以包含一个或者多个test_ 开头的的函数;
  4. 在执行pytest命令时,会自动从当前目录及子目录下寻找符合上述规范的测试函数来执行;

四、执行测试

# file_name: test_add.py

import pytest


def test_add01():
    print("test_add01")
    assert 1 == 1


if __name__ == '__main__':
    pytest.main(["-s","test_add.py"])

运行上面代码:

  pytest使用 . 来表示测试通过(PASSED)表示测试失败(FAILED);

原文地址:https://www.cnblogs.com/lwjnicole/p/14361872.html