04-02 unitest-安装与运行
测试开发 unittest 测试框架
安装与运行
unittest的入门学习
-----作者 老男孩教育 Robert讲师
前言
unittest是个框架,会不会安装起来很费力?
我听说java的sprint cloud框架安装要30分钟呢,那unittest框架也要这么久?
肯定还是有人对框架不是很了解。
对于软件工程的框架,不熟悉的化,我们可以联系别的行业来理解。
可以翻看前一页的unittest--00引入。
安装
Windows :
无需额外安装,python的lib中默认已有unittest
Linux:
无需额外安装,python的lib中默认已有unittest
mac:
无需额外安装,python的lib中默认已有unittest
![](D:\老男孩IT教育\讲师Robert\讲师修炼IT教育\测试学科\测试开发备课知识点\测试学科-- 测试开发--06unitest--01安装与运行\unittest在lib中.png)
验证方法:
验证方法一:import 导包
C:\Users\Robert>python
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import unittest
>>>
验证方法二:上代码运行
提前准备代码
import unittest
class MyTestCase(unittest.TestCase):
def test_something(self):
self.assertEqual(True, False)
if __name__ == '__main__':
unittest.main()
执行代码 python file.py
(testops) C:\Users\Robert\PycharmProjects\testops>python Stage5\06unittest\entry.py
F
======================================================================
FAIL: test_something (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "Stage5\06unittest\entry.py", line 6, in test_something
self.assertEqual(True, False)
AssertionError: True != False
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
(testops) C:\Users\Robert\PycharmProjects\testops>
另一种执行代码的方式 python -m unittest file.py
(testops) C:\Users\Robert\PycharmProjects\testops>python -m unittest Stage5\06unittest\entry.py
F
======================================================================
FAIL: test_something (Stage5.06unittest.entry.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\Robert\PycharmProjects\testops\Stage5\06unittest\entry.py", line 6, in test_something
self.assertEqual(True, False)
AssertionError: True != False
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)
过程描述:
1 用pycharm打开一个项目
2 在这个项目中新建python文件
3 选择unittest文件
![](D:\老男孩IT教育\讲师Robert\讲师修炼IT教育\测试学科\测试开发备课知识点\测试学科-- 测试开发--06unitest--01安装与运行\unittest安装.png)
1 安装了python的机器都已经有了Unittest 。不看不知道,一看吓一跳。
2 unittest这个测试框架已经普及到互联网世界的各个角落。
为什么你还是不知道呢?因为你已经Out了
运行
unittest单元测试框架运行的方法有许多种,上文中所展示的代码是命令行运行。
命令行模式,也是最根本的运行方式。
# 运行整个py文件的用例
python -m unittest Stage5\06unittest\entry.py
# 运行py文件指定的用例
python -m unittest Stage5\06unittest\entry.py MyTestCase
图形化运行模式
我们还可以通过IDE图形化来运行,也可以把通过接口来调用。
举个例子: 通过pycharm的图形界面来执行那就是IDE图形化来运行
http接口方式
再举个例子: 我们通过调用http接口,从而把test_something这个用例来执行,那就是接口调用的方法来执行。
这里我们先做个引入,具体的内容咱门下回再继续。有兴趣的可以自己先查阅相关资料做些预习。
讲到这里,大家都跃跃欲试了。那就先练习一下。
其他参数的参考:
unittest supports these command-line options:
-
`-b````, ``--buffer```
The standard output and standard error streams are buffered during the test run. Output during a passing test is discarded. Output is echoed normally on test fail or error and is added to the failure messages.
-
`-c````, ``--catch```
Control-C during the test run waits for the current test to end and then reports all the results so far. A second Control-C raises the normal
KeyboardInterrupt
exception.See Signal Handling for the functions that provide this functionality. -
`-f````, ``--failfast```
Stop the test run on the first error or failure.
-
`-k```
Only run test methods and classes that match the pattern or substring. This option may be used multiple times, in which case all test cases that match of the given patterns are included.Patterns that contain a wildcard character (
*
) are matched against the test name usingfnmatch.fnmatchcase()
; otherwise simple case-sensitive substring matching is used.Patterns are matched against the fully qualified test method name as imported by the test loader.For example,-k foo
matchesfoo_tests.SomeTest.test_something
,bar_tests.SomeTest.test_foo
, but notbar_tests.FooTest.test_something
. -
`--locals```
Show local variables in tracebacks.
New in version 3.2: The command-line options -b
, -c
and -f
were added.
New in version 3.5: The command-line option --locals
.
New in version 3.7: The command-line option -k
.