Python+selenium Note (iii): Use of UnitTest

Source: Internet
Author: User

#网络很慢可能会运行报错

(i) Preface

Selenium Webdriver is a collection of APIs for browser automation testing. It provides many features that interact with browser automation, and these APIs are primarily used to test web programs. If we only use selenium webdriver, we cannot implement the test preconditions, test the post conditions, compare the expected results and actual results, check the status of the program, generate test reports, create data-driven tests and other functions.

(ii) UnitTest Unit Testing Framework

UnitTest (commonly referred to as Pyunit) is inspired by JUnit, which is widely used in Java program development. UnitTest gives us the ability to create test cases, test suites, and test fixtures.

(iii) Xunite testing Framework

Arrange: is a precondition for initializing the test, including initializing the object being tested, related configuration, and dependencies.

ACT: Used to perform functional operations.

Assert: Used to verify that the actual result is consistent with the expected result.

(iv) TestCase class

Create a single test or set of tests by inheriting the TestCase class and adding test methods to each test in the test class.

(v) Use of unittest

(1) Define a subclass that inherits from the TestCase class.

(2) define the Setup () method in the class.

(3) Writing test methods

(4) Code Cleanup (define the teardown () method in the Class)

(5) Run the test

Description

1. A test case is from the setup () method starts, we can use this method to perform some initialization tasks before each test starts. This can be the initial preparation: Creating browser instances, accessing URLs, loading test data, and opening log files. This method has no parameters and does not return any values. When a setup () method is defined, the test executor takes precedence of the method each time the test method executes.

2. with Setup () method, you can write some tests to verify the functionality of the program that we want to test. The test method name begins with test , which notifies Test runner which method represents the test method.

3. similar to setup () method is called before each test method, TestCase class will also call Teardown () after the test execution is complete method to clean up all the initialization values. Once the test is executed, the values defined in the setup () method are no longer required, so it is best to clean up the values initialized by the Setup () method when the test execution is complete .

(vi) Create a new directory data, creating a file Category.txt, enter the following data

Programming languages, Java,c++,php,delphi,python,ruby,c languages, Erlang,go,swift,scala,r languages, Verilog, other languages

(vii) test the Blog Park home Page The classification information in the programming language is consistent with the expected results

Import UnitTest

fromSeleniumImportWebdriver
fromSelenium.webdriver.common.action_chainsImportActionchains
ImportTime
classSearchtest (unittest. TestCase):
defSetUp (self):
Self.driver= Webdriver. Firefox ()
Self.driver.implicitly_wait (10)
Self.driver.maximize_window ()
Self.driver.get ("https://www.cnblogs.com/")

defTest_search_by_category (self):
#read the Category.txt file and return a dictionary
withOpen' Data/category.txt ', encoding=' UTF-8 ') asCategory_file:
Category_dict = Dict ()
Category_data =category_file.readline (). Strip (). Split (', ')
The_class =category_data.pop (0)
Category_dict[the_class] =category_data
#Locate the programming language in the home site category
Self.seach_class = Self.driver.find_element_by_xpath ('//li/a[@href = "/cate/2/"] ')
#The cursor hovers over the programming language
Actionchains (Self.driver). Move_to_element (Self.seach_class). Perform ()
# returns all small classes under the programming language as a list
Seach_small = Self.driver.find_elements_by_xpath ( '//div[@id = ' cate_content_block_2 ']/div[@class = ' cate_content_block ']/ul/li ')
#Sleep 3 seconds
Time.sleep (3)
Small_cate = []
forSinchSeach_small:
#Remove the 3 characters (0) from the back of the small class and add them to the list small_cate
Small_cate.append (str (s.text) [:-3])
#checks if the expression is true (here, check if the small class under the programming language matches the expected result)
Self.asserttrue (small_cate = = category_dict["programming Language "])
# self.assertequal (small_cate,category_dict["programming Language "])

defTearDown (self):
Self.driver.quit ()
#Add the following 2 sentences, you can run the test from the command line, without affecting the test run through the IDE
if__name__ = =' __main__ ':
#Add verbosity=2 parameter to display the specific test method on the command line
Unittest.main (verbosity=2)

(eight) The test results are as follows

Run from the command line:

Python+selenium Note (iii): Use of UnitTest

Related Article

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.