[Python unittest] 2-Command-Line Interface

  • 命令行界面

我们可以通过命令行,控制Test Runner,运行测试模块、类或甚至具体某个方法

可以通过以下格式

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method

 我们的tests.py 是放在users文件下的,所以相对我们的例子就是

python -m unittest users.tests
python -m unittest users.tests.TestStringMethods
python -m unittest users.tests.TestStringMethods.test_upper

 运行结果:

python -m unittest users.tests
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
python -m unittest users.tests.TestStringMethods
...
----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
python -m unittest users.tests.TestStringMethods.test_upper
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

 unittest 的-v参数可以显示更加详细的过程

python -m unittest -v users.tests.TestStringMethods
test_isupper (users.tests.TestStringMethods) ... ok
test_split (users.tests.TestStringMethods) ... ok
test_upper (users.tests.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
  •  更多的unittest可选参数

python -m unittest -b users.tests.TestStringMethods
...
----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
python -m unittest -c users.tests.TestStringMethods
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
python -m unittest -f users.tests.TestStringMethods
...
----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK

 -b:标准输出和标准错误流在测试运行期间被缓冲。通过测试的输出被丢弃,测试失败或错误时正常响应,并添加到失败消息中。

 -c:文档写第一个ctrl+c会等到测试运行结束再执行,第二个立即抛出KeyboardInterrupt错误,但我自己试的时候是直接抛KeyboardInterrupt错误

 -f:在第一个错误或者失败的时候停止测试

  •  多个模块的测试用例

    discover 可以同时执行 规定路径下规定模块下的测试用例的执行

    python -m unittest discover -s '/home/fgzhang/Neoclub/miaohong-server/users' -p "tests.py"
    ...
    ----------------------------------------------------------------------
    Ran 3 tests in 0.000s
    
    OK
    
    
    python -m unittest discover -s '/home/fgzhang/Neoclub/miaohong-server/' -p "tests.py"
    ......
    ----------------------------------------------------------------------
    Ran 6 tests in 0.001s
    
    OK
    
    
    python -m unittest discover -s '/home/fgzhang/Neoclub/miaohong-server/' -p "tests.py" -v
    test_isupper (users.tests.TestStringMethods) ... ok
    test_split (users.tests.TestStringMethods) ... ok
    test_upper (users.tests.TestStringMethods) ... ok
    test_isupper (task.tests.TestStringMethods) ... ok
    test_split (task.tests.TestStringMethods) ... ok
    test_upper (task.tests.TestStringMethods) ... ok
    
    ----------------------------------------------------------------------
    Ran 6 tests in 0.004s
    
    OK
    

     

     

     discover 有四个参数,其中s ,p 必选

    -v, --verbose

    详细输出

    -s, --start-directory directory

    规定路径 (.默认)

    -p, --pattern pattern

    匹配规则 (test*.py 默认)

    -t, --top-level-directory directory

    顶层目录的名称(没什么用)

posted @ 2017-08-25 15:44  懒惰的咕噜  阅读(308)  评论(0编辑  收藏  举报