美文网首页
断言Assert,测试用例的运行,Python Console,

断言Assert,测试用例的运行,Python Console,

作者: 一二三开花 | 来源:发表于2020-10-29 11:38 被阅读0次
一、Assert断言

if

        try:
            self.assertEqual(1, 2-2)
        except AssertionError as e:
            #如果你想断言失败,一定要抛出AssertionError
            print('断言失败{}'.format(e))
            raise AssertionError   #raise  告诉系统受到异常信号 相当于认为制造了一个异常, 在raise后面的语句不再执行。
          assert (code != 418)
              print("123successful")
二、测试文件(unittest)的运行

定义一个测试类,文件名要以test开头 ,类要以大写Test开头,要继承于unittest.TestCase
要运行:
运行:右击:出现unittest,如果没有出现需要配置

使用python运行,在终端Python test.py

以python模式运行,必须写上此代码,表示以脚本运行的方式才执行,unitest执行的话这句代码不执行
if __name__ == "__main__":
    unittest.main()
三、结果:E表示代码有错误。F表示断言失败。.(圆点表示断言成功)
截屏2020-10-29 上午11.35.11.png [图片上传中...(截屏2020-10-29 上午11.35.56.png-5a453a-1603942601257-0)] 截屏2020-10-29 上午11.35.56.png
四、Python Console和Terminal

右键有run file in Python Console
其中,Python Console叫做Python控制台,即Python交互模式;Terminal叫做终端,即命令行模式。

Python交互模式主要有两种:CPython用>>>作为提示符,而IPython用In [序号]:作为提示符。

Python交互式模式可以直接输入代码,然后执行,并立刻得到结果,因此Python交互模式主要是为了调试Python代码用的。

命令行模式与系统的CMD(命令提示符)一样,可以运行各种系统命令。

五、

# if __name__ == "__main__":
print("程序正在运行")
unittest.main()

运行结果:没有测试用例,unittest运行两次,混合使用的bug


截屏2020-10-29 上午11.46.39.png

相关文章

网友评论

      本文标题:断言Assert,测试用例的运行,Python Console,

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