The Talent500 Blog
Python Testing Framework 1

Python Testing Framework

Python Testing Framework

What Is Python?

Python is an interpreted, high-level generic programming language that assists programmers in writing manageable and logical code for both small and large-scale projects.

The benefits of Python:

  •             The Edit-Test-Debug cycle runs quickly since there is no compilation
  •             Debugging is easy
  •             Extensive support library
  •             Easy learning data-structure
  •             High productivity
  •             Team collaboration

Working In Python

Python Testing Framework 2

  • The interpreter reads and evaluates the Python code in the source file for syntax errors.
  • If the code is error-free, the interpreter translates it to its ‘Byte code’ counterpart.
  • The byte code is then sent to the Python Virtual Machine (PVM), where it is compiled again for errors if any exist.

What is Python Testing

  • Automated testing is a well-known context in the testing field. It is when the test plans are conducted utilizing a script rather than a human.
  • Python includes tools and modules for automated testing of your system.
  • Python test cases are quite simple to develop. Python-based test automation frameworks are becoming more popular as the language gains popularity.

Overview of Python testing framework

The Python testing framework is a dynamic test automation framework created with the Python programming language, which is popular for web development and test automation. Because Python is an open-source programming language, several frameworks are built on it, making it difficult for testers to decide which test automation framework to choose among the many possibilities available.

List Of Python Testing Frameworks

Below are some Python Testing frameworks listed that you should know.

  1. Robot
  2. PyTest
  3. Unittest
  4. DocTest
  5. Nose2
  6.   Testify

1) Robot

  • The most popular Robot Framework is an open-source Python-based Automation Testing framework.,
  • Built fully in Python and used for Acceptance Testing and Test-driven Development. In the Robot framework, test cases are written in the keyword format.
  • The Robot can execute Java and.Net and also offers cross-platform automation testing for desktop apps, mobile applications, online applications, and so on.
  • Robot is utilized for Robotic Process Automation in addition to Acceptance Testing (RPA).
  • Pip (Python Package Installer) is strongly recommended for Robot installation.
  • Some of Robot’s strong characteristics that make it popular among testers are the usage of tabular data syntax, keyword-driven testing, a comprehensive library and tool set, and parallel testing.

 

Example:

Settings

Library: SeleniumLibrary

 Variables

${SERVER}         localhost:7272

${BROWSER}        Firefox

${DELAY}        0

${VALID USER} demo

${VALID PASSWORD} mode

${LOGIN URL}  http://${SERVER}/

${WELCOME URL} http://${SERVER}/welcome.html

${ERROR URL}  http://${SERVER}/error.html

 

Open Browser To Login Page

    Open Browser    ${LOGIN URL} ${BROWSER}

    Maximize Browser Window

    Set Selenium Speed ${DELAY}

Login Page Should Be Open

    Title Should Be Login Page

 

Go To Login Page

    Go To ${LOGIN URL}

    Login Page Should Be Open

 

Input Username

    [Arguments]    ${username}

    Input Text    username_field ${username}

 

Input Password

    [Arguments]    ${password}

    Input Text    password_field ${password}

 

Submit Credentials

    Click Button login_button

 

Welcome Page Should Be Open

    Location Should Be ${WELCOME URL}

    Title Should Be Welcome Page

Sample of Failed Test Execution

Python Testing Framework 3

A sample of Successful Test Execution

Python Testing Framework 4

2) PyTest

  • PyTest is a free and open-source Python-based testing framework that is ideal for functional and API testing.
  • Pip (Python Package Installer) is required for PyTest installation.
  • It allows you to test APIs, databases, and user interfaces using simple or complicated text code.
  • Simple syntax facilitates test execution.
  • Has a large number of plugins and can run tests in parallel. • Can execute any specified group of tests.

 

Example:
import pytest                                //Import unittest module//

def test_file1_method():               //Function inside class//

      x=5

      y=6

      assert x+1 == y,”test failed”

Use the py.test command to run the test.

Python Testing Framework 5Use the below command If you want to access a test written in a specific file.

py.test <filename>

Define PyTest fixture as shown below.

@pytest.fixture

Example

def test_string_equal():

assert double(55) == 62

assert 25 == 62

+  where 25 = double(55)

3) Unittest

  •             Unittest is the first Python-based automated unit test framework built to operate with the Python standard library.
  •             Encourages the reuse of test suits and the organizing of tests.
  •             It was inspired by JUnit and provides test automation such as test collections, test independence, test setup code, and so on.
  •             It is also known as PyUnit.
  •             Unittest2 is a backport of further new Unittest capabilities.

Workflow of Unittest:

  •             In the program code, include the Unittest module.
  •           The option of creating your own class.
  •             Create functions within the Class you’ve established.
  •             To execute the test case, place unittest.main(), which is the main method, near the bottom of the code.

Example:

import unittest                                 //Import unittest module//

def add(x,y):

   return x + y

 class Test(unittest.TestCase):          //Define your class with testcase//

   def addition(self):

      self.assertEquals(add(4,5),9)<strong>//Function inside class//

if __name__ == ‘__main__’:

   unittest.main()<strong>//Insert main() method//

Python Testing Framework 6

4) DocTest

  •             Doctest is a module provided in Python’s standard distribution that is used for White-box Unit Testing.
  •             It looks for interactive Python sessions to see whether they are functioning properly.
  •             It makes use of certain Python features including docstrings, the Python interactive shell, and Python introspection (determining properties of objects at runtime).
  •             The functions testfile() and testmod() are used to provide basic interface.

Core Functions:

  •             Updating docstring
  •             Performing Regression Testing

Example:

def test(n):

import math

    if not n >= 0:

        raise ValueError(“n must be >= 0”) //number should be 0 or greater than 0

    if math.floor(n) != n:

               raise ValueError(“n must be exact integer”)  

                                                                     //Error when number is not an integer

  if n+1 == n: 

        raise OverflowError(“n too large”) //Error when number is too large

    r = 1

    f = 2

    while f <= n:                                      //Calculate factorial

        r *= f

        f += 1

    return r

 

if __name__ == “__main__”:

     import doctest                        //Import doctest

    doctest.testmod()                    //Calling the testmod method

Python Testing Framework 7

Use the testfile () function to check interactive examples in the text file.

doctest.testfile (“example.txt”)

Run the test directly from the command line with;

python factorial.py

5) Nose2

  •             Nose2 is the successor to Nose and is a Python-based Unit Testing framework capable of running Doctests and UnitTests.
  •             Nose2 is built using unittest. It is known as extending unittest or unittest with the plugin, and it was created to make testing simpler and easier.
  •             Nose employs collective tests from unittest. Testcase and provides a variety of tools for writing tests and exceptions.
  •             Nose allows package fixtures, classes, modules, and complicated initialization to be declared once rather than repeatedly.

 

Example:

from mynum import *

import nose

 

def test_add_integers():

    assert add(5, 3) == 8

 

def test_add_floats():

    assert add(1.5, 2.5) == 4

 

def test_add_strings():

    nose.tools.assert_raises(AssertionError, add, ‘paul’, ‘carol’)

// To throw one of the expected exception to pass

 if __name__ == ‘__main__’:  

    nose.run()

Python Testing Framework 8

6) Testify

  •             Testify was created to take the role of unittest and nose. Testify provides more features than unittest.
  •             Testify is well-known as a Java semantic testing implementation (Easy to learn and implement software testing specification).
  •             It is simpler to testify while performing automated unit, integration, and system testing.

 Features

  •             Simple syntax to fixture method.
  •             Improvised test discovery.
  •             Class-level setup and teardown fixture method.
  •             Extensible plugin system.
  •             Easy to handle testing utilities.

Example:
 

from testify import *

 class AdditionTestCase(TestCase):

     @class_setup

    def init_the_variable(self):

        self.variable = 0

     @setup

    def increment_the_variable(self):

        self.variable += 1

    def test_the_variable(self):

        assert_equal(self.variable, 1)

     @suite(‘disabled’, reason=’ticket #123, not equal to 2 places’)

    def test_broken(self):

        # raises ‘AssertionError: 1 !~= 1.01’

        assert_almost_equal(1, 1.01, threshold=2)

     @teardown

    def decrement_the_variable(self):

        self.variable -= 1

     @class_teardown

    def get_rid_of_the_variable(self):

        self.variable = None

 if __name__ == “__main__”:

run()

Python Testing Framework 9

There are additional lists of frameworks that may become popular in the future.

7) Behave

  •             Behave is a test framework for BDD (Behavior Driven Development) that is also used for Black box testing. Behave writes tests in plain language and works with Unicode Strings.
  •             The Behave category contains feature files in plain text format that resemble natural language and Python step implementations.

8) Lettuce

  • Beneficial for testing Behavior Driven Development. It simplifies and scales the testing process.
  •     Lettuce steps:

o   Describing behavior

o   Steps definition in Python.

o   Running the code

o   Modifying code to pass the test.

o   Running the modified code.

These processes are repeated three or four times to ensure that the program is error-free and hence of high quality.

0
Subhojit Hazra

Subhojit Hazra

He is a tech enthusiast and a passionate marketer with an eye for detail. He loves to uncomplicate things and debate on business problems. A quiet guy who likes peaceful evenings and iced coffees.

Add comment