zoukankan      html  css  js  c++  java
  • 【pytest】1.安装&简单使用

    1、安装pytest

    pip安装,打开Windows的cmd,输入:pip install -U pytest

    pytest --version:检查是否安装成功

    2、pytest的第一个测试

    只用四行代码创建一个简单的测试函数:

    # content of test_sample.py
    def func(x):
        return x + 1
    
    def test_answer():
        assert func(3) == 5
    

    在test_sample.py的同级目录下输入命令pytest即可执行测试。

    pytest运行规则:查找当前目录及其子目录下的test_*.py*_test.py文件,找到文件后,在文件中找到以test开头函数并执行。

    3、测试类

    当有多个测试用例时,可以将它们分组写到一个测试类里。要确保在类前加上前缀Test,否则该类将被跳过。

    # test_class.py
    class TestClass:
        def test_one(self):
            x = "this"
            assert 'h' in x
    
        def test_two(self):
            x = "hello"
            assert hasattr(x, 'check')
    

    pytest会找到符合规则的所有测试文件。 如果只想运行其中一个,可以指定传递文件名test_class.py来运行模块:

    pytest -q test_class.py  # -q --quiet:decrease verbosity(显示简单结果)
    

    4、pytest用例设计规则

    1、文件名:test_*.py*_test.py
    2、函数:以test_开头
    3、类和方法:以Test_开头,并且不能带有__init__ 方法
    4、包pakege:必须要有__init__.py文件
    5、断言:使用assert

    5、Pytest收集测试用例的规则

    1)从一个或多个目录开始查找,可以在命令行指定文件或目录。如果未指定就从当前目录开始收集用例;

    2)在该目录和所有子目录下递归查找【测试模块】(文件名为test_*.py*_test.py的文件)

    3)在测试模块中查找以test_开头的函数

    4)查找名字以Test_开头的类。会先筛选掉包含__init__()函数的类,再查找类中以Test_开头的类方法

  • 相关阅读:
    BZOJ1222: [HNOI2001]产品加工(诡异背包dp)
    洛谷P1208 [USACO1.3]混合牛奶 Mixing Milk(贪心)
    SDOI 2018划水记
    【Leetcode】Search in Rotated Sorted Array II
    HDU 4089 Activation
    linux scp ssh命令不用输入密码
    封装fastjson为spring mvc的json view
    codility上的练习(3)
    git 拉取远程分之到本地
    Oracle 索引扫描的4种类型
  • 原文地址:https://www.cnblogs.com/mind18/p/15621156.html
Copyright © 2011-2022 走看看