一句话来说下,unittest和pytest脚本在pycharm中应用根本是一样的。根本是两种:
第一种:间接运行脚本
- 【运行】-【Run】,抉择须要运行的脚本即可
第二种:抉择运行框架
- 【文件】-【设置】-【Python Integrated Tools】-【Default test runner】,抉择默认的运行框架即可:
- 比方抉择pytest,鼠标放在类或test结尾的办法上,并右键,
“运行(U)pytest in xx.py”的字样
- 写一个unittest框架的脚本,在
test_a
下新建一个脚本test_u.py
,脚本如下:
# -*- coding:utf-8 -*-# 作者:NoamaNelson# 日期:2021/9/3 17:13# 文件名称:test_u.py# 作用:xxx# 分割:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsonimport unittestclass TestU(unittest.TestCase): def test_one(self): money = 1000000 if money > 10000: print(f"你曾经领有了{money}块钱,曾经很富裕了!")if __name__ == "__main__": unittest.main()
- 咱们先在
if __name__ == "__main__":
上右键,以pytest运行,发现是能够运行的,如下:
test_u.py::TestU::test_one PASSED [100%]你曾经领有了1000000块钱,曾经很富裕了!============================== 1 passed in 0.02s ==============================
- 阐明,pytest是兼容unittest的框架的,此时咱们把运行默认框架改为unittest,再次运行,发现显示的是
“运行(U)unittests in xx.py”的字样