"Selenium2 Python Automation Test Practice" (--unittest) unit Test framework Analysis

Source: Internet
Author: User

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:

    • Optimization 1:

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.

    • Optimization 2

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.