UnitTest is the foundation for automated testing-a framework that is important!
Let's start by writing a test class ourselves:
1, the test class widthget.py:
# coding: utf-8
class Widthget:
def __init__(self, size=(10, 10)): self._size = size
def getSize(self): return self._size
def reSize(self, width, height): if width < 0 or height < 0:
raise ValueError, ‘illegal size‘ self._size = (width, height)
2. Test class:
# Coding:utf-8
From WidthgetImport Widthget
Import UnitTest
# classes that perform tests
ClassWidthgettestcase(UnitTest. TestCase):DefSetUp(self): Self.width = Widthget ()
DefTestsize(self): Self.assertequal (Self.width.getSize (), (, 10))
def testresize (self): Self.width.reSize (, ) self.assertequal (Self.width.getSize (), ( 20, 20))
def tearDown (self): self.width = Span>none
# construct test set
def Suite (): &NBSP;TC = UnitTest. TestSuite () tc.addtest (Widthget ( "Testsize")) tc.addtest (Widthget ( "testresize ")
return TC
if __name__ = = ' __main__ ': # Uni Ttest.main () runner = UnitTest. Texttestrunner () runner.run (Suite ())
Run the test file to see:
We do not need to write the method of constructing the test set separately, that is, we can put the suite () method directly into if __name__== ' __main__ ': Inside:
if __name__ == ‘__main__‘: tc = unittest.TestSuite() tc.addTest(Widthget("testSize")) tc.addTest(Widthget("testResize")) runner = unittest.TextTestRunner() runner.run()
Execute the test file and you can see that the use case is also available.
Test cases are named after the test, such as testing the login, the use case is named Testlogin, the test exit use case is named Testloginout, the above example of our writing can be so smooth through, the main reason is that my use case (Testsize, Testresize) are all beginning with test. This is the default rule for the UnitTest framework.
Next is the batch execution of use cases, which we introduced in the interface testing framework, and is specifically introduced, we can look at
Interface Test Basics--6th unittest module (i)
Interface Test Basics--6th UnitTest Module (ii)
Interface Test Basics--6th UnitTest Module (iii)
These are the methods of batch execution. The above we talk about this is a very low example, you can just look at the line, and then I will write a concrete selenium framework, we directly refer to it. If you are interested, you need to review this knowledge:
Interface Test Basics--First article smtplib sending text messages
Interface Test framework--fifth-use case and run
Htmltestrunner Show use case print content
Public search " Automated test practice" or scan the QR code below to add attention ~ ~ ~
"Selenium2 Python Automation Test Practice" (--unittest) unit Test framework Analysis