- 在pycharm中新建一个包unittest_org(其下含有一个init.py文件)
- 在包下新建一个org.py
- 拷贝官网的代码:
#! /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() 和执行其下面的两行代码是一样的效果
- 也可以在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>
网友评论