What is unittest in selenium Python?
What is the meaning of lines 3,16,17,18 and 19 which are highlighted with *. Can someone explain what they do? I am new to python and programming
import unittest
from selenium import webdriver
**class Iframe(unittest.TestCase):**
def setUp(self):
self.driver = webdriver.Firefox()
def test_Iframe(self):
driver = self.driver
driver.maximize_window()
driver.get('http://www.toolsqa.com/iframe-practice-page/')
iframe1 = driver.find_element_by_id('IF1')
driver.switch_to.frame(iframe1)
driver.find_element_by_name('email').send_keys('xyz')
driver.switch_to.default_content()
list = driver.find_elements_by_tag_name('iframe')
print(len(list))
**def tearDown(self):
self.driver.quit()**
**if __name__ == '__main__':
unittest.main()**
Solution 1:
As you have choosen to use Python’s unittest here is the relevant info:
-
import unittest
: You need to import the required unittest module as a mandatory measure. -
class Iframe(unittest.TestCase):
: The testcase class is inherited from unittest.TestCase. Inheriting from TestCase class is the way to tell the unittest module that this is a testcase. -
def setUp(self):
: The setUp is the part of initialization and this method will get called before every test function which you are going to write in this testcase class. -
def test_Iframe(self):
: This is the actual testcase method. The testcase method should always start with the characters test. -
def tearDown(self):
: The tearDown method will get called after every test method. This is the method to do all the cleanup actions. -
if __name__ == '__main__':
: This line sets the__name__
variable to have a value"__main__"
. If this file is being imported from another module then__name__
will be set to the other module's name. You will find a detailed discussion in What does if name == "main": do? -
unittest.main()
: Invokes the test functions from the configured module.
Note A: For more details see Using Selenium to write tests and Walk through of the example
Note B: Refer A module's name for complete details.
Why self
The first argument of every class method, including init
is always a reference to the current instance of the class. By convention, this argument is always named self. In the init
method, self
refers to the newly created object while in other class methods, it refers to the instance whose method was called.
Trivia
The self variable in python explained
Solution 2:
Only three lines in this code are highlighted with an *, but here's what they mean:
First line:
class Iframe(unittest.TestCase):
This is declaring the class for the functions (test_Iframe and tearDown) that follow. A class is used to create "objects" in object oriented programming. Think of the class as the abstraction of data/procedures, while the object is the particular instance of the class.
Next line:
def tearDown(self):
self.driver.quit()
This section first declares a function with the def
keyword, and the function quits the driver, which was set as:
driver = self.driver
driver.maximize_window()
driver.get('http://www.toolsqa.com/iframe-practice-page/')
in the test_Iframe()
function.
Final line:
if __name__ == '__main__':
unittest.main()
This section simply executes the main function of the program. More details on this can be found here.
Let me know if you have any more questions!