The Talent500 Blog

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:

Working In Python

What is Python Testing

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

 

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

A sample of Successful Test Execution

2) PyTest

 

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.

Use 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

Workflow of Unittest:

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//

4) DocTest

Core Functions:

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

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

 

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()

6) Testify

 Features

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()

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

7) Behave

8) Lettuce

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