3. The unit testing framework (unittest) 1 - TestCase

 

1. import unittest
  Unittest module is introduced;
 
2. class SearchTest (unittest.TestCase):
  Inherited TestCase define a subclass;
 
3. def setUp(self):
  The test method executor preferentially performed prior to each execution of the test method (to help ensure each test method can rely on the same environment);
  This method takes no parameters and returns no value;
 
4. def test_search_by_category(self):
  test () method is implemented in the TestCase class, the important point is the need to test methods named as the beginning of the test; this convention notify the test runner method which represents the test method;
 
5. self.assertEqual(1,len(products))
  Call assertEqual (expected value, the actual value) to verify the expected results;
  The results of this verification program search term and whether the returned matches the expected results;
 
6. def tearDown(self):
  TestCase class will be called tearDown () method to clean up all of the initial value after the test to complete;
  Once the test is performed, the value defined in setUp () method is no longer needed, so the best practice is to clean out the initialization by setUp () method when the value of the test to complete;
 
7.
  if __name__ == '__main__':
  unittest.main(verbosity=2)
  In order to test the command line by running, we can add a call to the main methods in the test case.
  Verbosity parameters to be passed to make detailed tests show the total amount in the console:
    verbosity is an option, means that the test result of the complexity of the information, there are three values
    0 (silent mode): You can only get the total test cases and the total of a total of 100 results of such failure 20 success 80
    1 (默认模式): 非常类似静默模式 只是在每个成功的用例前面有个“.” 每个失败的用例前面有个 “F”
    2 (详细模式):测试结果会显示每个测试用例的所有相关的信息
    并且 你在命令行里加入不同的参数可以起到一样的效果
    加入 --quiet 参数 等效于 verbosity=0
    加入--verbose参数等效于 verbosity=2
    什么都不加就是 verbosity=1
 

 

8. 改初始化和结束初始化的方法级别的方法为类级别的方法,类级别的方法,无论类中有多少个用例,都只会共用一个浏览器;
  @classmethod
  def setUpClass(cls):
   @classmethod
  def tearDownClass(cls):
 
9. 方法中的self指向类的实例,有点this的意思,不加self的变量是本地变量,加了就是实例变量;
 
10. 测试用例执行顺序解决办法:
  1. 测试套件
    testcase = unittest.TestLoader().loadTestsfromTestCase(测试类)
    suites = unittest.TestSuite()
    suites.addTest(class('testcase')
  2. TestCase的名称:按照1-9,A-Z,a-z
  3. 补充unittest的框架:如链接楼主的utx;

 

Guess you like

Origin www.cnblogs.com/noobzeng/p/10991844.html