码迷,mamicode.com
首页 > 其他好文 > 详细

【Pytest】pytest学习,Pycharm安装pytest

时间:2021-03-31 12:34:21      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:设置   width   path   highlight   lse   err   att   world   nts   

1、pycharm的安装

File -> Settings ->选择python的版本 ->点击加号

技术图片

选择当前的文件进行安装

技术图片

 

搜索pytest  安装当前文件

 技术图片

 

 装好之后 以pytest方式运行部分代码,需要改该工程设置默认的运行器:file->Setting->Tools->Python Integrated Tools->项目名称->Default test runner->选择pytest,pytest是可以兼容unittest框架代码的

技术图片

 

 

 

技术图片

 

 

输入当前简单的pytest测试代码

import pytest

class TestClass:
        def test_one(self):
            x = "this"
            assert ‘h‘ in x

        def test_two(self):
            x = "hello"
            assert hasattr(x, ‘check‘)

        def test_three(self):
            a = "hello"
            b = "hello world"
            assert a in b

if __name__ == "__main__":
    pytest.main([‘-q‘, ‘test_class.py‘])  #注意对照文件名测试  

 

测试结果

Testing started at 10:04 ...
G:\1.python\python3.9.0\python.exe "G:\1.python\pycharm202001\PyCharm 2020.1\plugins\python\helpers\pycharm\_jb_pytest_runner.py" --path G:/1.python/Zproject/aaaa/456.py
Launching pytest with arguments G:/1.python/Zproject/aaaa/456.py in G:\1.python\Zproject\aaaa

============================= test session starts =============================
platform win32 -- Python 3.9.0, pytest-6.1.2, py-1.9.0, pluggy-0.13.1 -- G:\1.python\python3.9.0\python.exe
cachedir: .pytest_cache
rootdir: G:\1.python\Zproject\aaaa
plugins: allure-pytest-2.8.24
collecting ... collected 3 items

456.py::TestClass::test_one 
456.py::TestClass::test_two PASSED                                       [ 33%]FAILED                                       [ 66%]
456.py:7 (TestClass.test_two)
self = <456.TestClass object at 0x000002F796DDF130>

    def test_two(self):
        x = "hello"
>       assert hasattr(x, ‘check‘)
E       AssertionError: assert False
E        +  where False = hasattr(‘hello‘, ‘check‘)

456.py:10: AssertionError
PASSED                                     [100%]
Assertion failed

456.py::TestClass::test_three 

================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________

self = <456.TestClass object at 0x000002F796DDF130>

    def test_two(self):
        x = "hello"
>       assert hasattr(x, ‘check‘)
E       AssertionError: assert False
E        +  where False = hasattr(‘hello‘, ‘check‘)

456.py:10: AssertionError
=========================== short test summary info ===========================
FAILED 456.py::TestClass::test_two - AssertionError: assert False
========================= 1 failed, 2 passed in 0.13s =========================

Process finished with exit code 1

Assertion failed

  

 

【Pytest】pytest学习,Pycharm安装pytest

标签:设置   width   path   highlight   lse   err   att   world   nts   

原文地址:https://www.cnblogs.com/chenxiaomeng/p/14600190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!