Python+selenium----unittest Unit Test framework

Source: Internet
Author: User

UnitTest is a unit test framework that is a unit test framework for Python programming. Sometimes it is called "Pyunit", which is the Python language version of JUnit. Here, JUnit is the Java Language Unit test framework, Java has a very useful unit test framework called TESTNG, this series only learn python, so only need to UnitTest is a unit test framework in Python.
UnitTest supports test automation, sharing initialization and closing exit codes in test cases, and in UnitTest, the smallest unit is test, which is a testing case. To understand the UnitTest unit testing framework, here are a few important concepts to understand first.
Test Firmware(Test fixture)
A test firmware consists of two parts, the preparation part before executing the test code, and the sweep code after the test is finished. These two sections are generally represented by the function setup () and teardown (). Here, for example, to test Baidu search Selenium this scenario, our test firmware can write this, setUp () to open the browser, browser maximization, and open the Baidu homepage and other script code, in teardown () write the end of the search, exit and close the browser code.
Test Cases(Test case)
The smallest unit managed in UnitTest is a test case, a test case, including a test firmware, and a function or method that specifically tests the business. In a test case, the test firmware may not be written, but there is at least one function that begins with test. UnitTest will automatically identify the function at the beginning of the test code, if you write a function is not the beginning of test, UnitTest will not execute the script inside the function, this must be remembered, all the test function to the beginning, remember that is lowercase oh.
test Suite(Test suite)
Very simply, a collection of many test cases, called a test suite, where a test suite can manage multiple test cases at will. If the test case is likened to a single student, the test suite is like a class concept.
Test Actuator(Test Runner)
Test Runner is a build that executes the load test case, executes the use case, and provides the test output. Test runner can load tests case or testing suite to perform the task.

For example, let's practice the use of test fixture and test case to learn the simple usage of unittest:
1. Create a new testbaidu.py file
2. Import the UnitTest module
3. The current test class inherits UnitTest. TestCase, equivalent to the current use of unittest to create a test case, the test case can be directly identified by UnitTest.
4. Write Setup (), mainly open the browser and open the site
5. Write a test_search () use case to write the search code
6. Write Teardown (), mainly browser exit operation

The relevant script code is as follows:

# coding=utf-8import timeimport unittestfrom Selenium Import Webdriverclass Baidusearch (unittest. TestCase): Def setup (self): "" "The Code for the test firmware setup () is primarily a prerequisite for testing: return:" "" self.drive R = Webdriver. Chrome () Self.driver.maximize_window () self.driver.implicitly_wait (8) self.driver.get ("https://www.ba Idu.com ") def TearDown (self):" "" After the end of the test operation, here is basically close the browser: return: "" "Self.driver.qui        T () def test_baidu_search (self): "" "It is important to test the beginning of the code, to encapsulate the testing logic into a method at the beginning.            : Return: "" "self.driver.find_element_by_id (' kw '). Send_keys (' Selenium ') time.sleep (2) Try:        Assert ' Selenium ' in self.driver.title print (' Test Pass. ') Except Exception as E:print (' Test Fail. ', Format (e)) if __name__ = = ' __main__ ': Unittest.main () 

At the end of the Unittest.main (), add this is supported in CMD, inside, CD to the directory where this script file resides, and then Python The script name. PY execution, if not add this paragraph, is unable to execute CMD inside run script, in Pycharm, do not add the last paragraph, also can pass, right click Run "UnitTest xxx", to achieve the effect of execution.

Python+selenium----unittest Unit Test framework

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.