Selenium as we know, is an open-source test suite used for cross-platform browser automation. Due to its popularity and ease of use, many compatible test frameworks with respect to different programming languages have been developed to extends support of Selenium for cross browser testing over multiple programming languages. Today we’re going to look into Selenium Python tutorial to run our first automation script using PyUnit framework with Selenium.
PyUnit is a unit testing framework which is derived from JUnit for compatible execution of Selenium with Python language. PyUnit is a very popular testing framework to perform ‘Unit Testing’ – a mechanism to validate a particular piece of functionality in a module. The popularity of PyUnit as a Python specific unit testing framework has been a major reason for including PyUnit as an official Python module from version 2.5. The Python UnitTest library referred to as unittest is widely used to perform Selenium Python Testing. We will leverage the unittest library in this Selenium Python tutorial.
Let’s start the Selenium Python tutorial by understanding the building blocks of PyUnit framework. The unittest framework contains the following core-modules that are core to test case development & execution:
TestCase class is used to create new tests and the FunctionTestCase acts as a subclass to TestCase class and make use of tests which are appended to the existing unittest framework. setUp() and tearDown() are important components of the TestCase class that are used for initialization & cleanup of the test fixture (that was created using setUp()). The primary purpose of using FunctionTestCase class is to aid the goal of code reusability.
PyUnit also referred as unittest and works in a similar manner as xUnit which is a very popular unit testing framework whose structure & functionality is derived from Smalltalk’s SUnit. Hence, a developer who has prior experience with xUnit or other popular unit testing frameworks would find unittest easier to understand.
Testcase/Testsuite written using unittest module follows a common format
We would have a look at the important classes in the unittest package in further sections.
The setUp() method is an entry point to the test cases. It does not have any arguments. Since it is the entry point, some of the important initialization steps are performed in the implementation of the setUp() method. Below are some things related to initialization that can be included in setUp()
Below is an example of the initialization & cleanup activity performed via setUp() and tearDown() methods.
import unittest
#Import other modules that are required for testing
class SearchText(unittest.TestCase):
def setUp(self):
#create a new FireFox session
self.driver = webdriver.FireFox()
.................................
.................................
self.driver.get("http://www.lambdatest.com")
def tearDown(self):
.................................
.................................
self.driver.quit()
In the above example, an instance of Firefox browser is created using the WebDriver API (Line 7). On successful creation, the Home Page LambdaTest is opened on the Firefox browser. All the necessary operations which are required for unit testing are then performed. Once the unit test is complete, the cleanup is performed in the tearDown() API (Line 12). In a nutshell, setUp() & tearDown() are executed before & after each every test method.
Now that you have understood the basics of the initialization & de-initialization of a Unit test case written in Python, let’s have a look at some of the important classes in this selenium python tutorial.
class unittest.TestCase(methodName=’TestName’)
We would have a look at the important classes in the unittest package in further sections of this Selenium Python tutorial. Specific test cases are implemented in subclasses. In many scenarios, no changes would be required in methodName nor runTest() needs to be implemented. We have already discussed the important initialization methods setUp() and tearDown(), now we would have a look at the other methods.
A class method is called before any of the individual tests are called. @classmethod is the identifier with which you can identify a setUpClass(). setUpClass() has only one argument i.e. the class name.
This method is called after all the tests in the class are executed. Similar to setUpClass(), tearDownClass() also as only one argument i.e. the class name. The ‘class name’ should match with the name which is used in the setUpClass(), else it might result in an error. A sample implementation citing the usage of setUpClass() and tearDownClass() is shown below:
import unittest
class Example(unittest.TestCase):
@classmethod
def setUpClass(class_name):
print("setUpClass executed")
def setUp(self):
print("setUp executed")
def test_1(self):
print("test-1 executed")
def test_2(self):
print("test-2 executed")
def tearDown(self):
print("tearDown executed")
@classmethod
def tearDownClass(class_name):
print("tearDownClass executed")
One question that could arise – What is the basic difference between setUp() and setUpClass() [or tearDown() & tearDownClass()]? setUp() [and its de-initialization counterpart tearDown()] are loaded & executed before each test method, whereas setUpClass() [and its de-initialization counterpart tearDownClass()] are executed once for the whole class. Let’s have a look at a simple example to understand the difference.
As seen in the below example [SetupClass-Python.py], depending on the test case being executed [test_1 or test_2], setUpClass gets executed once in the beginning and then the corresponding test case is executed. After the execution of the test case, the code in tearDownClass() is executed.
In order to execute the code, press CTRL+F9. As seen in the snapshot below, both the test cases observed in this Selenium Python tutorial were executed in the below sequence of execution:
As explained earlier, setUpClass() & tearDownClass() are executed only once, whereas setUp() & tearDown() are executed for each test method.
TestResult object is passed as a parameter to the run() method. The parameter is optional and if the object is not supplied to the method, a temporary object is created & used to store the results. The result is passed to the caller of the run() method. We would discuss the TestResult object in detail in a subsequent section.
The TestSuite class is an aggregation of individual testcases & test suites. Instead of executing testcases on an iterative basis, developers & testers can make use of this class since makes the code maintenance easy.
Unlike the TestCase() class, the TestSuite() class does not contain any implementation of test code since it is used to group testcases on a logical/functional basis. Some of the methods that can be used along with TestSuite() class are mentioned below
TestResult class is used to gather information about the number of test cases that have passed/failed/skipped. As discussed earlier, the TestCase & TestSuite classes are used to record the results of the testcase/testsuite execution. Some of the important attributes of TestResult are failures, errors, skipped, expectedFailures, unexpectedSuccesses.
Assertions are very popular in testing, irrespective of the programming language being used for test-code development. An assertion is nothing but a Boolean expression (representing ‘True/False’) which would carry a value ‘True’ till the time it encounters a bug/issue in the test code. Three types of assertions are available to check equivalence, comparison, and performing actions in case of exceptions.
Next in this Selenium Python tutorial, we will have a look at some of the important assertions for PyUnit framework.
With the basics of PyUnit/unittest covered, let’s have a look at a sample code which can delve into the aspects which have discussed so far
'''This sample program demonstrates the PyUnit/unittest framework'''
# Inclusion of the PyUnit Test Framework
import unittest
def addition(x,y,z=0):
# Add the three parameters which are passed to the addition function
return x+y+z
# This is where the test case is implemented
class AddTest(unittest.TestCase):
def setUp(self):
# initialization code for the testcase/testsuite can be added here
pass
# These are tests that should be performed once the basic premise is set
# Unit Tests start with test_
# The addition method which was implemented earlier will be used here
def test_addtion(self):
self.assertEqual(addition(10,11,12), 33)
# x=11, y=12, z=44 if (x+y)=z, the test would raise an assert since
# the test is for assertNotEqual operation
self.assertNotEqual(addition(11,12), 44)
def test_negative_values(self):
self.assertEqual(addition(-9,25), 16)
self.assertNotEqual(addition(-9,25), 17)
def tearDown(self):
# Deinit and cleanup should be done here
pass
if __name__=='__main__':
unittest.main()
The above code comprises of two testcases – test_addition() and test_negative_values(). A method named addition() [Line 6~ Line 8] is created to add three parameters that are passed to that method. assertNotEqual and assertEqual are used in the testcases. Respective Testcases would pass if the conditions in the Assert equate to TRUE.
When the above code is executed, the final test result is PASS as conditions mentioned in assertEqual and assertEqual are fulfilled e.g. In test_addition() testcase, assert would be invoked if addition of x=10,y=11,z=12 does not equate 33 since it is testing for ‘Equate’ operation. Below is the screenshot of the output when the code is compiled & tested from the command prompt [file – Python-unittest-output1.png]
In order to invoke an Assert, we make a small change in the test_negative_values() testcase. As you can see below in this Selenium Python Tutorial, the snapshot of the output.
'''This sample program demonstrates the PyUnit/unittest framework'''
# Inclusion of the PyUnit Test Framework
import unittest
def addition(x,y,z=0):
# Add the three parameters which are passed to the addition function
return x+y+z
# This is where the test case is implemented
class AddTest(unittest.TestCase):
def setUp(self):
# initialization code for the testcase/testsuite can be added here
pass
# These are tests that should be performed once the basic premise is set
# Unit Tests start with test_
# The addition method which was implemented earlier will be used here
def test_addtion(self):
self.assertEqual(addition(10,11,12), 33)
# x=11, y=12, z=44 if (x+y)=z, the test would raise an assert since
# the test is for assertNotEqual operation
self.assertNotEqual(addition(11,12), 44)
def test_negative_values(self):
self.assertNotEqual(addition(-9,25), 16) #Changed Values#
def tearDown(self):
# Deinit and cleanup should be done here
pass
if __name__=='__main__':
unittest.main()
As seen on Line-27, assert would be issued if (x+y)!=z. In our example, x=-9, y=25, and z=16. As the test condition fails, it issues assert. Below is the snapshot of the output
This Selenium Python tutorial helped us to understand some of the important aspects of PyUnit. Especially how this effective Unit testing framework can be used to automate cross browser testing code and how ‘asserts’ can be used effectively while testing your code.
PyUnit can also be used to perform module-level testing since you also have the flexibility to execute corner test cases as well. During the course of the article, we used Notepad++ & ‘command line Python’ for writing PyUnit code & execution. As a developer, you should use the IDE & programming environment of your choice & comfort.
Though PyUnit was written for ‘C’ Python, it is now possible to write PyUnit tests for languages like Java using the Jython framework. Covering Jython is beyond the scope of this article. You can find the PyUnit/unittest official documentation here.
Previously published at https://www.lambdatest.com/blog/using-pyunit-for-testing-a-selenium-python-test-suite/