Wednesday 17 December 2014

Unit Testing With Python

Unit Test Framework
Selenium is used for Web UI automation ,
To write and to maintain test cases a framework is required.
Here in this case  Unitest is used for this purpose

About Unittest

  • Unittest module started life as the third-party module PyUnit.
  • Unittest support fixtures,test suites and a test runner to enable automated testing.


Core Class of unittest
  1. TestCase 
  2. TestSuite
  3. TestLoader
  4. TextTestRunner
  5. TestResults
TestCase
  • TestCase is a one of the core classes of unittest module.
  • TestCase has to be inherited from unittest module
  • All the test routines or methods must start with "test".
  •  Only then will they be executed, Test case can be in a different file,if so the module to be tested must be imported.

 


Example 1


Unit test for lowercasevowel.py


Output


Note: each "." represents testcase successfully executed ,if test case fails "F" instead of "."


Structure of TestCase Class of unittest 

  1. setUp() where pre-condition for executing the test case are written down.
  2. tearDown() here clean up ,which is required after each test case is written down.
  3. skipTest() will skip the current test.
  4. fail() will fail the test explicitly ie..It will fail the test an raise AssertionError.
  5. id() Return a string containing the name of the TestCase object and of the test method.
  6. shortDescription() Return the Docstring present in the Test case method.
Note:setUp() get executed before all the test_case() method and tearDown() after each test_case()
methods.

Example 2
 Let us use the setUp() and tearDown() methods





Example 3
 Let us use the setUp() and tearDown() id() & shortDescription() methods




Problem 1
Search for a python tutorial on youtube ?

Answer

  1. Open a browser (firefox)
  2. Nagivate to "http://www.youtube.com"
  3. Verify that youtube page is opened
  4. Locate the search Box and type in Python Tutorials
  5. To take a screenshot

The complete solution with unittest as Framework

 


Output




1 comment: