标签:设置 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