0
点赞
收藏
分享

微信扫一扫

pytest学习和使用3-对比unittest和pytest脚本在pycharm中运行的方式


3 对比unittest和pytest脚本在pycharm中运行的方式

  • ​​第一种:直接运行脚本​​
  • ​​第二种:选择运行框架​​


一句话来说下,unittest和pytest脚本在pycharm中使用基本是一样的。基本是两种:

第一种:直接运行脚本

  • 【运行】-【Run】,选择需要运行的脚本即可
  • pytest学习和使用3-对比unittest和pytest脚本在pycharm中运行的方式_python


  • pytest学习和使用3-对比unittest和pytest脚本在pycharm中运行的方式_pycharm_02

第二种:选择运行框架

  • 【文件】-【设置】-【Python Integrated Tools】-【Default test runner】,选择默认的运行框架即可:
  • 比如选择pytest,鼠标放在类或test开头的方法上,并右键,​​“运行(U)pytest in xx.py”的字样​
  • pytest学习和使用3-对比unittest和pytest脚本在pycharm中运行的方式_sed_03


  • pytest学习和使用3-对比unittest和pytest脚本在pycharm中运行的方式_python_04


  • 写一个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/NoamaNelson


import unittest

class 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”的字样​
  • pytest学习和使用3-对比unittest和pytest脚本在pycharm中运行的方式_pycharm_05


『全栈测试技术,分享,共勉,共进,提升』


pytest学习和使用3-对比unittest和pytest脚本在pycharm中运行的方式_单元测试_06


举报

相关推荐

0 条评论