Unit testing framework unittest
unit test
Test unit (unit testing) refers to the smallest testable software unit checking and validation. For the meaning of the unit test unit, in general, be determined according to the actual situation of its specific meaning, such as C language, refers to a function unit, Java classes in a cell refers to, the graphical software may refer to a window or a menu Wait. In general, the smallest unit measured is a predetermined function module human.
Unit testing framework
Before unit testing framework, developers were tortured when creating executable test. The initial approach is to create a window in an application, with "test control tool (harness)". It is just a window, a button corresponding to each test. The results of these tests are either a message box, either directly in the form itself is given some sort of display the results. Since each test requires a button, these windows will soon become crowded and unmanageable.
Unit testing framework provides a unified programming model, the test may be defined as a simple class, the class of these calls application code may want to test. Developers do not need to write your own test control tools; unit testing framework provides a test runner (runner), just click the button to perform all tests. Using a test frame unit, you can easily be inserted, and decomposition function arranged on testing. When the test fails, run the test program can provide information about the failure, including any exception information available for use and stack trace. Different programming languages have different unit testing frameworks such as Java's Junit, TestNg, c # of Nunit, Python's unittest, Pyunit, testtools, subunit ....
Action unit testing framework
- Example providing organization and execution
- Provide a wealth of assertions
- It provides a wealth of logs and test results
Python unit testing framework --unittest
unittest official documents https://docs.python.org/2.7/library/unittest.html
unittest unit test frame may be applicable not only to the test unit, can be applied WEB automated test development and execution of the test frame may be organized to execute test cases, and provides a rich assertions, by determining whether the test case, the test results to generate the final .
unittest core elements
1.TestCase
A TestCase instance is a test case. What is the test case of it? Is a complete testing process, including building (setUp) to prepare the environment before the test, the test code execution (run), and the reducing environment of the test (tearDown). Nature yuan test (unit test) is also here, a test case is a complete test unit, one can verify this problem by running the test unit.
2.TestSuite
And a plurality of test cases together, is TestSuite, but can also be nested TestSuite TestSuite. TestLoader is used to load into TestSuite TestCase in which there are several loadTestsFrom __ () method, that is, from various places to look TestCase, create instances of them, then add the TestSuite, and then returns a TestSuite instance.
3.TextTestRunner
TextTestRunner test is performed, wherein the run (test) performs TestSuite / TestCase the run (result) method. Test results are saved to TextTestResult examples, including running a number of test cases, the number of successful, failed and so much information.
4.Fixture
The destruction of the building and a test environment, is a fixture.
unittest Case
Math class contains a configuration integer adder
class Math: def __init__(self,a,b): self.a=int(a) self.b=int(b) def add(self): return self.a+self.b
Math class of the unit test
from caculator import Math import unittest class TestMath(unittest.TestCase): def setUp(self): print("Start test") def test_add(self): j=Math(5,10) self.assertEqual(j.add(),15) #用例失败场景 # self.assertEqual(j.add(),12) def tearDown(self): print("test end") if __name__=='__main__' : # Configured test set Suite = unittest.TestSuite () suite.addTest (TestMath ( " test_add " )) # test is performed Runner = unittest.TextTestRunner () runner.run (Suite)
Affirmation
Content is an important assertion automated script, later set correctly assert can help us to judge the results of the test case.
Assertion methods
- assertEqual(a, b) 判断a==b
- assertNotEqual (a, b) A determination! = B
- assertTrue(x) bool(x) is True
- assertFalse(x) bool(x) is False
- assertIs(a, b) a is b
- assertIsNot(a, b) a is not b
- assertIsNone(x) x is None
- assertIsNotNone(x) x is not None
- assertIn(a, b) a in b
- assertNotIn(a, b) a not in b
- assertIsInstance(a, b) isinstance(a, b)
- assertNotIsInstance(a, b) not isinstance(a, b)
import unittest from calculator import * class TestMath(unittest.TestCase): def setUp(self): print("test is start") def add_test(self): j = Math(5,10) self.assertEqual(j.add(),10) def add_test1(self): j = Math(5,10) self.assertNotEqual(j.add(),10) def add_test2(self): j = Math(5,10) self.assertTrue(j.add()>10) def assertIs_test(self): self.assertIs("51zxw",'51zxw') # self.assertIs("51zxw",'abc') def assertIn_test(self): self.assertIn("51zxw","hello,51zxw") self.assertIn("888","hello,51zxw") def tearDown(self): print("Test end") if __name__=='__main__': suite=unittest.TestSuite() suite.addTest(TestMath("assertIn_test")) runer=unittest.TextTestRunner() runer.run(suite)
New Use Case Management
Front to add unit tests for a single method if you need more methods to test, how to deal with? As a method to add a Sub Unit test validation.
class Math: def __init__(self,a,b): self.a=int(a) self.b=int(b) def add(self): return self.a+self.b def sub(self): return self.a-self.b
While the add unit testing and verification methods Sub
from calculator import * import unittest class Test_add(unittest.TestCase): def setUp(self): print("Test is start") def test_add(self): j=Math(5,5) self.assertEqual(j.add(),10) def test_add1(self): j=Math(10,20) self.assertEqual(j.add(),30) def tearDown(self): print("test is end!") class Test_sub(unittest.TestCase): def setUp(self): print("Test is start") def test_sub(self): i=Math(8,8) self.assertEqual(i.sub(),0) def test_sub1(self): i=Math(5,3) self.assertEqual(i.sub(),2) def tearDown(self): print("test is end!") if __name__ == '__main__': suite=unittest.TestSuite() suite.addTest(Test_add("test_add")) suite.addTest(Test_add("test_add1")) suite.addTest(Test_sub("test_sub")) suite.addTest(Test_sub("test_sub1")) runner=unittest.TextTestRunner() runner.run(suite)
Combined with the common part Example
On a course content, each test class has SetUp () and tearDown () method, and the contents of the two methods are the same for print start and end precautionary statements, whether it can be merged together?
from calculator import * import unittest class Test_StarEnd(unittest.TestCase): def setUp(self): print("test start") def tearDown(self): print("test end") class Testadd(Test_StarEnd): def test_add(self): j=Math(5,5) self.assertEqual(j.add(),10) class TestSub(Test_StarEnd): def test_sub(self): i=Math(3,2) self.assertEqual(i.sub(),1) if __name__ == '__main__': unittest.main()
Example execution order with
Observe the following test scripts, test case execution order thinking.
import unittest class Test1(unittest.TestCase): def setUp(self): print("Test1 start") def test_c(self): print("test_c") def test_b(self): print("test_b") def tearDown(self): print("test end") class Test2(unittest.TestCase): def setUp(self): print("Test2 start") def test_d(self): print("test_d") def test_a(self): print("test_a") def tearDown(self): print("Test2 end!")
The order of execution rules - Test Method Test class or order numbers and letters 0 ~ 9, AZ
An integrated framework for managing cases
Test is performed with the front written in a document, when the number of cases with increasing time, with the execution and management becomes very troublesome cases, it is necessary to use depending on the particular embodiment of the functional module to use a separate module to manage. Like a school placement should be managed according to different grades, it is the same reason.
Case: Under Test_Project file directory contains four python file:
- l - StartEnd.py-- SetUp and management TearDown
- l - implemented method for computing subtraction calculatory.py--
- l - test_add.py-- adder test
- l - test_sub.py-- subtraction test
- l - runtest.py-- use case execution management
calculatory.py
# Two add or subtract the number of class the Math: DEF the __init__ (Self, A, B): self.a = int (A) self.b, = int (B) DEF the Add (Self): return self.a + Self. B DEF Sub (Self): return self.a-self.b,
StartEnd.py
import unittest class SetUp_TearDown(unittest.TestCase): def setUp(self): print("test start") def tearDown(self): print("test end")
test_add.py
from calculator import * from StartEnd import * class TestAdd(SetUp_TearDown): def test_add(self): i=Math(5,5) self.assertEqual(i.add(),10) def test_add1(self): i=Math(8,8) self.assertEqual(i.add(),16)
test_sub.py
from calculator import * from StartEnd import * class TestSub(SetUp_TearDown): def test_sub(self): i=Math(5,5) self.assertEqual(i.sub(),0) def test_sub1(self): i=Math(8,5) self.assertEqual(i.sub(),3)
runtest.py
- Use discover more scripts can be called once
- test_dir the path of test scripts
- pattern matching rules script name
import unittest test_dir='./' discover=unittest.defaultTestLoader.discover(test_dir,pattern='test*.py') if __name__ == '__main__': runner=unittest.TextTestRunner() runner.run(discover)
Skip test and failed expectations
Overview
- unittest.skip () Test skip
- unittest.skipIf () is true, the test is skipped
- unittest.skipUnless condition is false, skip the test
- unittest.expectedFailure expected setup failed
skip rules set Case
import unittest class Test1(unittest.TestCase): def setUp(self): print("Test1 start") @unittest.skipIf(4 > 3, "skip Test_d") def test_c(self): print("test_c") # @unittest.skipUnless(1<0, "skip test_b") def test_b(self): print("test_b") def tearDown(self): print("Test1 end") @unittest.skip(" skip Test_2") class Test2(unittest.TestCase): def setUp(self): print("Test2 start") def test_d(self): print("test_d") # @unittest.expectedFailure def test_a(self): print("test_a") def tearDown(self): print("Test2 end!") if __name__ == '__main__': unittest.main()
Web writing test cases
Case: Baidu search keywords: "Selenium self-learning network" and open the course page.
from selenium import webdriver import unittest from time import sleep class TestBaidu(unittest.TestCase): def setUp(self): self.driver=webdriver.Firefox() self.driver.implicitly_wait(10) self.driver.get("http://www.baidu.com/") def test_baidu(self): driver=self.driver driver.find_element_by_id("kw").clear() driver.find_element_by_id("kw").send_keys("Selenium自学网") driver.find_element_by_id("su").click() sleep(3) title=driver.title self.assertEqual(title,"Selenium自学网_百度搜索") driver.find_element_by_partial_link_text("Selenium自动化").click() sleep(5) def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
Implementation of test cases
import unittest test_dir='./test_case' discover=unittest.defaultTestLoader.discover(test_dir,pattern="test*.py") if __name__ == '__main__': runer=unittest.TextTestRunner() runer.run(discover)
Test report generation
After the automated test execution is complete, we need to generate test reports to view test results, using HTMLTestRunner module can generate reports Html format directly.
download link:
http://tungwaiyip.info/software/HTMLTestRunner.html
After downloading the Review:
- The line 94 is introduced to the name change, change from import io import StringIO.
- 539行 self.outputBuffer = StringIO.StringIO() 要改成self.outputBuffer=io.StringIO()
- 631行 print >>sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)
修改为:print (sys.stderr, '\nTime Elapsed: %s' %(self.stopTime-self.startTime))
- 642 line, if not rmap.has_key (cls): need to be replaced if not cls in rmap:
- 766 rows uo = o.decode ( 'latin-1'), into uo = o
- Line 772, the ue = e.decode ( 'latin-1') directly into ue = e
Storage path
The modifications are complete Python modules stored in the directory path to Lib
Import the unittest from HTMLTestRunner Import HTMLTestRunner Import Time # define test path test_dir = ' ./test_case ' Discover = unittest.defaultTestLoader.discover (test_dir, pattern = " Test * .py " ) IF the __name__ == ' __main__ ' : # store report folder report_dir = ' ./test_report ' # reporting format designated time now the time.strftime = ( " % D%% Y-M-% H_% of M_% S " ) # report file full path + = report_dir report_name ' / ' + now + ' result.html ' # Open the file write test results in the report file with Open (report_name, ' wb ' ) AS f: runer = HTMLTestRunner (Stream = f, title = " the Test Report " , = Description ' the Test Result Case ' ) runer.run (Discover) f.close ()
Test Report beautification
Download: https://github.com/easonhan007/HTMLTestRunner
note:
Also need to be modified and, like the previous content download, and then placed in the path of the Lib file Python installation folder
import unittest from BSTestRunner import BSTestRunner import time test_dir='./test_case' discover=unittest.defaultTestLoader.discover(test_dir,pattern="test*.py") if __name__ == '__main__': report_dir='./test_report' now=time.strftime("%Y-%m-%d %H_%M_%S") report_name=report_dir+'/'+now+'result.html' with open(report_name,'wb')as f: runer=BSTestRunner(stream=f,title="Test Report",description='Test case result') runer.run(discover) f.close()
Reference Source:
http://blog.csdn.net/huilan_same/article/details/52944782
http://blog.csdn.net/qq1124794084/article/details/51668672
http://www.cnblogs.com/nbkhic/p/5914400.html
"Selenium2 automated real" - Mushishi