# Import unittest library for working with Selenium Unit Testing Python.
import unittest
from selenium import webdriver
# Import the page object files in the Pages directory.
# The POM files are MainPage.py and EnglishPage.py. Their code is in
Selenium Python tutorial 13.
from Pages.MainPage import MainPage
from Pages.EnglishPage import EnglishPage
class WikipediaTestClass1(unittest.TestCase):
exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
base_URL = "https://www.wikipedia.org/"
# First independent test method to test the main page
def test_main_page(self):
driver = webdriver.Firefox(executable_path=self.exec_path)
driver.get(self.base_URL)
mp = MainPage(driver)
mp.test_title()
# Second independent test method to test the english page
def test_english_page(self):
driver = webdriver.Firefox(executable_path=self.exec_path)
driver.get(self.base_URL)
mp = MainPage(driver)
mp.click_english_link()
ep = EnglishPage(driver)
ep.search_text()
# Run all test methods using the unit test library.
# In PyCharm, click the green arrow next to this if statement and then click Run 'WikipediaTestClass1'.
if __name__ == "__main__": unittest.main()
Next, in this Selenium WebDriver Python tutorial, let us see the Selenium Python example to skip test methods using Selenium Python. I modified the above Python script to skip test methods.
# Test_Cases>WikipediaTestClass2.py
# Selenium WebDriver Python coding
import unittest
from selenium import webdriver
from Pages.MainPage import MainPage
from Pages.EnglishPage import EnglishPage
class WikipediaTestClass2(unittest.TestCase):
# run_all_test_cases variable is used to skip unit tests conditionally.
run_all_test_cases = False
exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
base_URL = "https://www.wikipedia.org/"
# If you want to skip unit test with condition, use the skipIf unit test annotation.
@unittest.skipIf(run_all_test_cases==False, "This is a medium priority test case.")
def test_main_page(self):
driver = webdriver.Firefox(executable_path=self.exec_path)
driver.get(self.base_URL)
mp = MainPage(driver)
mp.test_title()
# If you want to skip unit test, uncomment the unit test annotation.
# @unittest.SkipTest
def test_english_page(self):
driver = webdriver.Firefox(executable_path=self.exec_path)
driver.get(self.base_URL)
mp = MainPage(driver)
mp.click_english_link()
ep = EnglishPage(driver)
ep.search_text()
if __name__ == "__main__":
unittest.main()
Finally, let us see the Selenium Python example with unit testing setup method and unit testing teardown method. I added these unit test special methods, the unit testing set up method and and unit testing tear down method to the above Python script.
# Test_Cases>WikipediaTestClass3.py
# Notice the @classmethod unit test annotation.
# Notice the @classmethod unit test annotation.
Want to see the Python unit test library run the above Selenium Python examples? Then view my Python Selenium UnitTest Tutorial and
. Thank you.