1.unittest的属性:
['BaseTestSuite', 'FunctionTestCase', 'SkipTest', '
TestCase
', 'TestLoader', 'TestProgram', 'TestResult', '
TestSuite
', 'TextTestResult', '
TextTestRunner
', '_TextTestResult', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__unittest', 'case', '
defaultTestLoader
', '
expectedFailure
', 'findTestCases', 'getTestCaseNames', 'installHandler', 'loader', '
main
', 'makeSuite', 'registerResult', 'removeHandler', 'removeResult', 'result', 'runner', 'signals', '
skip
', '
skipIf
', '
skipUnless
', 'suite', 'util']
1)unittest.TestCase--所有测试用例类继承的基本类
2)unittest.TestSuit--用来创建测试套件
3)unittest.TextTestRunner--通过该类下面的run()方法可以用来执行测试套件组装的测试用例,传入的参数是suit测试套件
4)unittest.defaultTestLoader--通过该类下面的discover()方法可以自动根据测试目录匹配查找测试文件(test*.py),并将查找到的测试用例组装到测试套件,因此可以直接通过run()方法运行discover
5)unittest.main--可以将单元测试模块变为可直接运行的脚本,main()方法使用TestLoader类来搜索包含在该模块中的以test*开头的测试方法,并自动执行它们。根据ASCII码的顺序加载执行用例0-9,A-Z,a-z
2.TestCase类的属性
['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_addSkip', '_baseAssertEqual', '_classSetupFailed', '_deprecate', '_diffThreshold', '_formatMessage', '_getAssertEqualityFunc', '_truncateMessage', 'addCleanup', 'addTypeEqualityFunc', 'assertAlmostEqual', 'assertAlmostEquals', 'assertDictContainsSubset', 'assertDictEqual', '
assertEqual
', 'assertEquals', '
assertFalse
', 'assertGreater', 'assertGreaterEqual', '
assertIn
', '
assertIs
', '
assertIsInstance
', '
assertIsNone
', '
assertIsNot
', '
assertIsNotNone
', 'assertItemsEqual', 'assertLess', 'assertLessEqual', 'assertListEqual', 'assertMultiLineEqual', 'assertNotAlmostEqual', 'assertNotAlmostEquals', '
assertNotEqual
', 'assertNotEquals', '
assertNotIn
', '
assertNotIsInstance
', 'assertNotRegexpMatches', 'assertRaises', 'assertRaisesRegexp', 'assertRegexpMatches', 'assertSequenceEqual', 'assertSetEqual', '
assertTrue
', 'assertTupleEqual', 'assert_', 'countTestCases', 'debug', 'defaultTestResult', 'doCleanups', 'fail', 'failIf', 'failIfAlmostEqual', 'failIfEqual', 'failUnless', 'failUnlessAlmostEqual', 'failUnlessEqual', 'failUnlessRaises', 'failureException', 'id', 'longMessage', 'maxDiff', 'run', '
setUp
', 'setUpClass', 'shortDescription', 'skipTest', '
tearDown
', 'tearDownClass']
1)setUp()--用于测试用例执行测试前的
初始工作。可以再setUp中建立数据库连接并进行初始化,实例化浏览器等
2)tearDown--测试用例执行后的善后工作。可以关闭数据库连接,关闭浏览器
3)assert*()--断言方法,最终用例是否通过,是通过测试结果和期望结果进行对比判断
(1)assertIn(a,b,[msg=测试失败时打印的信息])——断言a是否在b中,在b中则测试通过
(2)assertIs(a,b,[mag=测试失败时打印的信息])——断言a是b,是b则测试通过
(3)assertIsinstance(a,b,[msg=测试失败时打印的信息])——断言a是b的实例,是则测试通过
(4)assertNone(x,[msg=测试失败时打印的信息])——断言x是None,是则测试通过
(5)assertEqual(a,b,[msg=测试失败时打印的信息])——断言a=b,相等时则测试通过
(6)assertTrue(x,[msg=测试失败时打印的信息])——断言x是True,是则测试通过
3.TestSuit类的属性
['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_addClassOrModuleLevelException', '_get_previous_module', '_handleClassSetUp', '_handleModuleFixture', '_handleModuleTearDown', '_tearDownPreviousClass', '_tests', '
addTest
', '
addTests
', 'countTestCases', 'debug', 'run']
1)addTest()--将测试用例添加到测试套件中
suit = unittest.TestSuite()
‘’’suit.addTest(测试用例所在的类名(
"测试用例方法名"
))‘’’
suit.addTest(Login(
"test_user_login_001"
))
4.TextTestRunner类的属性
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_makeResult', 'buffer', 'descriptions', 'failfast', 'resultclass', '
run
', 'stream', 'verbosity']
1)run()--运行测试套件中的测试用例,入参是测试套件suit
suit = unittest.TestSuite()
suit.addTest(Login(
"test_user_login_001"
))
runner = unittest.TextTestRunner()
runner.run(suit)
5.defaultTestLoader类
1)discover()--自动根据测试目录匹配查找符合模式的测试用例文件(test*.py),并将查找到的测试用例组装到测试套件中
test_dirctory
=
"testcase_for_test"
#定义要运行的测试文件夹
#根据pattern自动匹配查找test_dirctory文件夹下的测试用例文件
testcase
*.
py
tests = unittest.defaultTestLoader.discover(
test_dirctory
, pattern=
"
testcase
*.
py
"
, top_level_dir=
None
)
#执行测试用例
runner = unittest.TextTestRunner()
runner.run(tests)
执行测试
方案1
#coding=
utf
-8
'''
Created on 2017年9月19日
@author:
caoyq
'''
import
unittest
from
pip._vendor.distlib.compat
import
raw_input
class
LearningTest
(unittest.TestCase):
def
setUp
(
self
):
self
.number = raw_input()
self
.number = int(
self
.number)
def
test_case1
(
self
):
print
(
self
.number)
if
self
.number ==
10
:
self
.assertEqual(
self
.number,
10
, msg=
"Your input is 10!"
)
else
:
self
.assertEqual(
self
.number,
10
, msg=
"Your input is not 10!"
)
def
test_case2
(
self
):
print
(
self
.number)
def
tearDown
(
self
):
print
(
"Game over!"
)
if
__name__ ==
"__main__"
:
unittest.main()
方案2
#coding=
utf
-8
'''
Created on 2017年9月19日
@author:
caoyq
'''
import
unittest
from
pip._vendor.distlib.compat
import
raw_input
class
LearningTest
(unittest.TestCase):
def
setUp
(
self
):
self
.number = raw_input()
self
.number = int(
self
.number)
def
test_case1
(
self
):
print
(
'test_case1='
,
self
.number)
self
.assertEqual(
self
.number,
10
, msg=
"Your input is not 10!"
)
def
test_case2
(
self
):
print
(
'
test_case2
='
,
self
.number)
self
.assertEqual(
self
.number,
20
, msg=
"Your input is not 20!"
)
def
tearDown
(
self
):
print
(
"Game over!"
)
if
__name__ ==
"__main__"
:
suit = unittest.TestSuite()
suit.addTest(LearningTest(
"test_case1"
))
suit.addTest(LearningTest(
"
test_case2
"
))
runner = unittest.TextTestRunner()
runner.run(suit)
方案3
1.将测试用例放在一个包testcase_for_test下
2.在与该包平级下创建一个模块run
run.py
#-*-coding:utf-8-*-
'''
Created on 2017年9月19日
@author:
caoyq
'''
import
unittest
test_dir =
"testcase_for_test"
tests = unittest.defaultTestLoader.discover(test_dir,
"testcase002.
py
"
)
runner = unittest.TextTestRunner()
runner.run(tests)