What is unittest in selenium Python?

99封情书 提交于 2019-12-02 13:23:21

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

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!