美文网首页
unittest命令行运行

unittest命令行运行

作者: 慧琴如翌 | 来源:发表于2018-03-10 17:55 被阅读295次
  1. 在pycharm中新建一个包unittest_org(其下含有一个init.py文件)
  2. 在包下新建一个org.py
  3. 拷贝官网的代码:
#! /usr/bin/python
#! coding=UTF-8
import unittest
class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)
if __name__ == '__main__':
    # unittest.main()
    #以上两句可以替换为下面两句
    suite = unittest.TestLoader().loadTestsFromTestCase(TestStringMethods)
    unittest.TextTestRunner(verbosity=2).run(suite)

备注:直接执行unittest.main() 和执行其下面的两行代码是一样的效果

  1. 也可以在command中敲命令
  • 下面是运行了一个类里面的用例,但是输出信息很简介,没有显示接口的名字
localhost:unittest_org fujunmin$ pwd
/Users/fujunmin/files/python/python_project/heyan/test-platform/src/com/chinadaas/platform/testlib/business/riskbell/test/unittest_org
localhost:unittest_org fujunmin$ ls
__init__.py __init__.pyc    org.py      org.pyc
localhost:unittest_org fujunmin$ python -m unittest org.TestStringMethods
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
  • 命令里加上 -v(如下面例子),即可以输出用例名字了
localhost:unittest_org fujunmin$ python -m unittest -v  org.TestStringMethods
test_isupper (org.TestStringMethods) ... ok
test_split (org.TestStringMethods) ... ok
test_upper (org.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
  • 下面的例子是只运行一条用例的例子
localhost:unittest_org fujunmin$ python -m unittest -v  org.TestStringMethods.test_upper
test_upper (org.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
localhost:unittest_org fujunmin$

<small>aafds</small>

相关文章

网友评论

      本文标题:unittest命令行运行

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