我们之前发布了Web自动化框架实践指南文章,并放出了一个简化版的自动化框架,有同学后台留言说,不知道那些测试用例是如何组织的,如何决定哪些用例要run,那些要skip,我们今天来解答下。
代码里可以看到,我测试用例的组织主要靠python unittest来完成。这个有点像junit,我们先来讲如何使用:
- 导入unittest模块
- 定义继承自unittest.TestCase的测试用例类
- 定义setUp和tearDown,setUp()方法中进行测试前的初始化工作, tearDown()方法中执行测试后的清除工作,setUp()和tearDown()都是TestCase类中定义的方法。
- 定义测试用例,名字以test开头。然后完成具体测试用例的编写,断言,判断等。
- 调用unittest.main()启动测试或者提供名为suite()的全局方法来启动测试。
举例如下:
以上是针对登录page的所有测试用例,在这些用例中,不需要执行的只需要在函数定义前加入如下代码即可:
来实现查找case并执行(一下代码基于所有的testcase放在一个名为tests的文件目录下):
扩展阅读:
unittest.skip(reason)
Unconditionally skip the decorated test. reason should describe why the test is being skipped.
unittest.skipIf(condition, reason)
Skip the decorated test if condition is true.
unittest.skipUnless(condition, reason)
Skip the decorated test unless condition is true.
unittest.expectedFailure()
Mark the test as an expected failure. If the test fails when run, the test is not counted as a failure.
discover(start_dir, pattern=’test*.py’, top_level_dir=None)
Find all the test modules by recursing into subdirectories from the specified start directory, and return a TestSuite object containing them.
Only test files that match pattern will be loaded. (Using shell style
pattern matching.) Only module names that are importable (i.e. are valid Python identifiers) will be loaded
All test modules must be importable from the top level of the project. If
the start directory is not the top level directory then the top level directory must be specified separately.
If importing a module fails, for example due to a syntax error, then this
will be recorded as a single error and discovery will continue.
If a test package name (directory with init.py) matches the
pattern then the package will be checked for a load_testsfunction. If this exists then it will be called with loader, tests,pattern.
If load_tests exists then discovery does not recurse into the package,load_tests is responsible for loading all tests in the package.
The pattern is deliberately not stored as a loader attribute so that packages can continue discovery themselves. top_level_dir is stored so load_tests does not need to pass this argument in toloader.discover().
start_dir can be a dotted module name as well as a directory.