Selenium2+python automation 51-unittest introduction

foreword

Those who are familiar with java should be aware of the common unit testing frameworks Junit and TestNG, and this recruitment requirement is often seen. There is also a unit test framework - unittest in python, which is equivalent to a python version of junit.

In addition to unittest, the unit testing framework in python also has a pytest framework, which is used less frequently, and will continue to be shared later when it is free.

 

1. Introduction to unittest

1. Import unittest first

2. Use the help function to view the source code analysis

3. Check the description:

Python unit testing framework, based on Erich Gamma's JUnit and Kent Beck's Smalltalk testing framework.

Translation: python's unit testing framework is a java-based junit testing framework

 

2. Simple usage

1. You can copy the code above, run it separately, and see the test results.

    Simple usage:
    
        import unittest
    
        class IntegerArithmeticTestCase(unittest.TestCase):
            def testAdd(self):  ## test method names begin 'test*'
                self.assertEqual((1 + 2), 3)
                self.assertEqual(0 + 1, 1)
            def testMultiply(self):
                self.assertEqual((0 * 10), 0)
                self.assertEqual((5 * 8), 40)
    
        if __name__ == '__main__':
            unittest.main()

2. The first line is to import the unittest module

The 3.class line is to define a test class and inherit the unittest.TestCase class

4. Next is to define two test case names: testAdd and testMultiply

5. There is a sentence in the comment that is very important. This one needs to be tapped on the blackboard to take notes: ## test method names begin 'test*'

--Translation : The name of the test case should start with test

6. Then there is the assertion, the assertion method here is assertEqual - to judge whether two are equal, this assertion can be one or more than one

7. If the unittest.main() below is to run the main function, you will see the test results after running (it took 0.000 seconds to run two use cases, and both use cases passed):

..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

 

Three, small test knife

1. The above two cases are addition and multiplication. We can write a case to try subtraction and division.

2. There are many friends who don't know how to write assertions. Assertions are actually comparing the actual results with the expected results. There are many ways to compare, but here is just the simplest way to judge equality.

3. The last running result, the second one failed, the reason for the failure: AssertionError: 3 != 3.5

F.
======================================================================
FAIL: testDivide (__main__.Test)
这里是测试除法
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:/test/web-project/p.py", line 14, in testDivide
    self.assertEqual(result, hope)
AssertionError: 3 != 3.5

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (failures=1)

Four, front and rear

1.setUp: When writing a test case, each operation is actually based on opening the browser and entering the corresponding URL. This is the precondition for executing the use case.

2. tearDown: After the use case is executed, in order not to affect the execution of the next use case, there is generally a data restoration process, which is the post-condition for executing the use case.

3. Many people do not perform data restoration after executing the use case, so that the execution of the next use case fails.

4. Pre and post are both non-essential conditions, if not, you can also write pass

 

5. Blog Case

1. Open the blog homepage as an example and write a simple case

2. Judging that the title is completely equal to the expected result

3. After the operation is passed, there will be a green bar below: 1 test passed

 

6. Reference code

# coding=utf-8
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
import time
import unittest

class Blog(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.get("http://www.cnblogs.com/yoyoketang")

    def test_blog(self):
        # 交流QQ群:232607095
        time.sleep(3)
        result = EC.title_is(u'上海-悠悠 - 博客园')(self.driver)
        print result
        self.assertTrue(result)

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325339319&siteId=291194637