Python+selenium Note (iv): UnitTest's test suite (Beta suite)

Source: Internet
Author: User

(i) Test Suite test Suite

A test suite is a collection of multiple tests or test cases, a set of tests that are created for the corresponding functions and modules of the program under test, and the test cases within a suite of tests are executed together.

Applying the testsuites feature of UnitTest, you can compose different tests into a logical group, set up a unified test suite, and execute the test with a single command. This is done through the testsuites, Testloader, and Testrunn classes.

(ii) the Setup () method and the Teardown () method at the class level

Use the SetupClass () method and the Teardownclass () method and the @classmethod identity to implement each test method to share the initialization data. See the code below for details.

(iii) searchtest.py

Import UnitTest

from Selenium import webdriver

from selenium.webdriver.common.action_chains import actionchainsimport time

classSearchtest (unittest. TestCase):
" "the SetupClass () and @classmethod logos enable
Initializes the data at the class level, and all test methods share the initialization data.
If you do not use this, each test method will create a separate instance "
@classmethod
defSetupClass (CLS):
Cls.driver = Webdriver. Firefox ()
Cls.driver.implicitly_wait (10)
Cls.driver.maximize_window ()
Cls.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.search_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.search_class). Perform ()
# returns all small classes under the programming language as a list
Self.search_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 = []
forSinchSelf.search_small:
#remove the back of the Small class (0) and add it to the list small_cate
small = str (s.text). Split (' (')
Small_cate.append (Small[0])
#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 "])
defTest_search_by_look (self):
Self.seach_class = Self.driver.find_element_by_xpath ('//li/a[@href = "/cate/2/"] ')
#Small class Python in the Location programming language
Self.seach_small =self.driver.find_element_by_xpath ('//li/a[@href = "/cate/python/"] ')
Actionchains (Self.driver). Move_to_element (Self.seach_class). Perform ()
Self.seach_small.click ()
#Check open page title is not Python-site category-Blog Park
# assert "Python-site categories-Blog Park" in Self.driver.title
Self.assertequal (Self.driver.title,"Python-site Category-Blog Park ")
" "the Teardownclass () and @classmethod logos enable
Initialize data at the class level, all test methods share these initialization data '
@classmethod
defTeardownclass (CLS):
Cls.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)
homepagetest.py
ImportUnitTest
fromSeleniumImportWebdriver
fromSelenium.common.exceptionsImportNosuchelementexception
fromSelenium.webdriver.common.byImportBy
classHomepagetest (unittest. TestCase):
" "the SetupClass () and @classmethod logos enable
Initializes the data at the class level, and all test methods share the initialization data.
If you do not use this, each test method will create a separate instance "
@classmethod
defSetupClass (CLS):
Cls.driver = Webdriver. Firefox ()
Cls.driver.implicitly_wait (10)
Cls.driver.maximize_window ()
Cls.driver.get ("https://www.cnblogs.com/")
defTest_search_field (self):
#through by, check the Blog Park homepage There is no search box, is_element_present () is a custom method
Self.asserttrue (Self.is_element_present (By.id,"Zzk_q"))
defTest_search_btn (self):
# through by, check the blog Park home Page There is no looking for the button
Self.asserttrue (Self.is_element_present (By.class_name,"SEARCH_BTN"))
defTest_menu (self):
#This method checks whether the information in the Blog Home menu bar is consistent with the expected
#读取menu. txt file data
withOpen' Data/menu.txt ', encoding=' UTF-8 ') asMenu_file:
Menu_data = Menu_file.readline (). Strip (). Split (', ')
#return to the blog Home menu bar information in list form
Self.check_menu = Self.driver.find_elements_by_xpath ('//div[@id = ' nav_menu ']/a ')
The_menu = []
forCinchSelf.check_menu:
#Add the menu name and URL to the list on the home page of the blog Park The_menu
The_menu.append (C.text + c.get_attribute (' href '))
#Check that the 2 lists are consistent (check that the menu name and URL are the same as expected on the homepage of the blog Park)
Self.assertlistequal (The_menu,menu_data)
defIs_element_present (self,how,what):
#find element, return true, otherwise return false
Try:
Self.driver.find_element (by=how,value = what)
exceptNosuchelementexception asE:
return False
Return True
@classmethod
defTeardownclass (CLS):
Cls.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)
(Test suite)
UnitTest
Searchtest
Homepagetest
# get all test methods in the Searchtest class and the Homepagetest class
Search_test = UnitTest. Testloader (). Loadtestsfromtestcase (Searchtest)
Home_page_test = UnitTest. Testloader (). Loadtestsfromtestcase (Homepagetest)
# Create a test suite that includes Searchtest and homepagetest
Smoke_tests = UnitTest. TestSuite ([home_page_test,search_test])
# Run the test suite
UnitTest. Texttestrunner (verbosity=2). Run (smoke_tests)

(vi) Other Notes

# Another document has been described in the previous article

To fully use the above code, you need to create a file Menu.txt and enter the following data:

Garden https://home.cnblogs.com/, News https://news.cnblogs.com/, Bo asked https://q.cnblogs.com/, Flash https://ing.cnblogs.com/, Group https://group.cnblogs.com/, collection https://wz.cnblogs.com/, recruitment https://job.cnblogs.com/, class https://edu.cnblogs.com/, Look for http://zzk.cnblogs.com/.

(vii) Run smoketests.py

Python+selenium Note (iv): UnitTest's test suite (Beta suite)

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.