美文网首页收藏
Pytest学习(2)简单练习+测试

Pytest学习(2)简单练习+测试

作者: 九千年小妖 | 来源:发表于2021-11-23 21:50 被阅读0次

    上篇讲述的pytest的特点及使用规则,并安装pytest type。

    接下来开始我的学习
    一、首先创建一个项目,在项目里面创建一个pytest可以识别出来的文件(以test_开头的.py文件)
    image
    二、接下来开始自由发挥,写一些简单的代码:
    #首先导入pytest库
    import pytest
    
    #创建一个类,开头以Test开头
    class TestDemo:
        #创建一个函数,以test_开头
        def test_one(self):
            x = "this"
            assert 'h' in x
    
        def test_two(self):
            a = "hello"
            b = "hello world"
            assert a in b
    
    
    if __name__ == "__main__":
        pytest.main('-q test_class.py')
    
    三、用pytest执行这个代码

    注:当脚本命名为test_xx.py时,pycharm默认用到unittest框架,此时运行代码,pycharm会自动识别到以unittest方式运行,pytest是可以兼容unittest框架代码的,那么这时你可以:
    首先去设置pycharm按pytest识别:File | Settings | Tools | Python Integrated Tools,找到Testing,如下:


    image.png

    然后就可以执行上方的测试脚本了


    image.png

    运行发现Pass,这个脚本正常通过:


    image.png

    相关文章

      网友评论

        本文标题:Pytest学习(2)简单练习+测试

        本文链接:https://www.haomeiwen.com/subject/kwvbzktx.html