Python中的pytest的使用

使用pytest可以做测试

pip 安装

pip install pytest

有可能得配置环境变量!


下面代码展示
文件名必须得是 test_ 开头 或者 _test 结尾

import pytest

def add(x, y):
    return x + y

def test1():
    assert 3 == add(1, 2)

def test2():
    assert 2 == add(1, 1)

if __name__ == '__main__':
    pytest.main(['-v', 'test_func.py'])

在 VS code 里点击运行按钮,就能看到测试输出了。


如果不想使用上面的例子,可以通过

python -m pytest

来运行代码,但是代码得稍微修改

def add(x, y):
    return x + y

def test1():
    assert 3 == add(1, 2)

def test2():
    assert 2 == add(1, 1)


点个赞支持一下呗~

相关推荐

  1. Pythonpytest使用

    2024-07-14 08:46:05       27 阅读
  2. pytest钩子函数使用

    2024-07-14 08:46:05       33 阅读
  3. Pytest钩子函数

    2024-07-14 08:46:05       22 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 08:46:05       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 08:46:05       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 08:46:05       58 阅读
  4. Python语言-面向对象

    2024-07-14 08:46:05       69 阅读

热门阅读

  1. Power BI 工具介绍

    2024-07-14 08:46:05       24 阅读
  2. 【C语言】多线程服务器

    2024-07-14 08:46:05       22 阅读
  3. 数学建模如何创新

    2024-07-14 08:46:05       27 阅读
  4. 【Qt】使用临时对象的坑

    2024-07-14 08:46:05       22 阅读
  5. C++智能指针的用法

    2024-07-14 08:46:05       24 阅读
  6. vue怎样自定义指令?

    2024-07-14 08:46:05       22 阅读
  7. c++【入门】求平均分

    2024-07-14 08:46:05       22 阅读