Test Automation using Python – Part 1

In this post we will see how you can write test automation using Python. We see how to configure your Python test environment and write your first Python unit test using unittest framework.

Setup and Configuration

  • You will need to install Python (go for latest version). On Mac it is by default installed but may not be having latest version. It is recommended to install latest version. https://www.python.org/downloads/

Note: On Windows machine during setup On the “Advanced Options” screen, check “Add Python to environment variables”

  • Also, make sure to install PIP using following command. This will be used later to install additional packages like selenium package.
python get-pip.py
  • Install an editor to work on your files. If on windows, I recommend to used Visual Studio Code. You can also use notepad++

Unittest framework

In Python there are may testing framework which you can use to write your test cases. I will show here unittest which is default Python testing framework and is part of standard Python library. Unittest framework support concept of test fixture, test cases, test suit, test runner.

Like other test framework you have setup() and teardown() methods. We will see more on it when writing selenium tests or can refer the official documentation here.

Some key features are:

  1. Support for Setup(), teardown(), setUpClass(), tearDownClass(), run(),debug() etc.
  2. Skip specific test using @unittest.skip and has many variance to it. You can skip classes also based on condition. For example, you can skip some code based on platform or python version
  3. Organize test cases in test suit using TestSuit class
  4. You can write subTest() under your test. Classic case of running test with different test data.
  5. Support for following variation of asserts
    • assertEqual(a, b)
    • assertNotEqual(a, b)
    • assertTrue(x)
    • assertFalse(x)
    • assertIs(a, b)
    • assertIsNot(a, b)
    • assertIsNone(x)
    • assertIsNotNone(x)
    • assertIn(a, b)
    • assertNotIn(a, b)
    • assertIsInstance(a, b)
    • assertNotIsInstance(a, b)

Writing your Sample function to unit test

Lets take an example of following code to find factorial of a number written in file factorialfindv2.py

# Return the value of factorial of number if it is defined, otherwise return None

def factorial(num):
    if type(num)==int:
        if num>0:
            return num*factorial(num-1)
        if num==0:
            return 1
    else:
        return None

Tests are normally written in a different file and in our case I have written in factorialunittest.py We can write many test scenarios for it as shown below

from factorialfindv2 import factorial
import unittest
class TestFactorial(unittest.TestCase):
    def test_smallinteger(self):
        #factorial of a valid integer
        self.assertEqual(factorial(5),120)
    def test_zero(self):
        #factorial is 1 for 0
        self.assertEqual(factorial(0),1)
    def test_char(self):
        #Should return none if text is passed
        self.assertEqual(factorial('saas'),None)
    if __name__ == '__main__':
        unittest.main()
  1. Note the first line where we are importing the method from the file factorialfindv2.py and importing unittest
  2. Also the TestFactorial class is inheriting from unittest.TestCase class.
  3. All test case begins with key word ‘test’
  4. To run the unit test file use the following command in your terminal
    • python -m unittest .\factorialunittest.py -v

                result of running the command can be seen below:

Unit_Test_Result

In the next part we will write selenium test with setup and teardown methods.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.