zoukankan      html  css  js  c++  java
  • Pytest学习笔记(三) 在代码中运行pytest

    前面介绍的是在cmd中执行pytest,平常我们一般都是通过编译器(如pycharm)来编写用例的,写完用例后,需要调试看看是否能运行,如果每次都切换到cmd中执行,太麻烦。

    因此,这一节来说下怎么在代码中执行pytest。

    需要先导入pytest,并通过pytest.main()来执行。

    import pytest
    class TestClass(object):
        def test_one(self):
            x = "this"
            assert 'h' in x
    
        def test_two(self):
            x = "hello"
            assert hasattr(x, 'check')
    
    if __name__ == '__main__':
        pytest.main()

    默认是执行当前脚本所在目录下的所有用例。

    当然,也可以加参数来指定运行规则,参数必须放在列表或元组中

    import pytest
    class TestClass(object):
        def test_one(self):
            x = "this"
            assert 'h' in x
    
        def test_two(self):
            x = "hello"
            assert hasattr(x, 'check')
    
        def test_three(self):
            assert 3 == 5
    
    if __name__ == '__main__':
        pytest.main(['-q','--maxfail=1','test_class.py'])

  • 相关阅读:
    win10系统封装
    docker基础知识
    TCP三次握手顺手看个示例
    磁盘挂载
    jQuary
    docker rpm包安装
    Mysql单机安装
    docker网络模式
    JavaScript DOM
    JavaScript作用域,面向对象
  • 原文地址:https://www.cnblogs.com/eastonliu/p/10731712.html
Copyright © 2011-2022 走看看