Python Unittest | Pyunit
Python Unit Test Module or
PyUnit is a unit test framework similar to Junit .The unittest module provides classes that
make it easy to support these qualities for a set of tests. Helps in
creating easy Automation and handling big codes !
How to install :
Pip install
unittest ---- Step 1
Unit Test Features :
- supports test automation
- sharing of setup and shutdown code for tests
- aggregation of tests into collections
- independence of the tests from the reporting framework
Basic Functions in UnitTest :
test fixture
A test
fixture represents the preparation needed to perform one or more
tests, and any associate cleanup actions. This may involve, for example,
creating temporary or proxy databases, directories, or starting a server
process.
test case
A test case is
the smallest unit of testing. It checks for a specific response to a particular
set of inputs. unittest provides a base class, TestCase,
which may be used to create new test cases.
test suite
A test suite is
a collection of test cases, test suites, or both. It is used to aggregate tests
that should be executed together.
test runner
A test runner is
a component which orchestrates the execution of tests and provides the outcome
to the user. The runner may use a graphical interface, a textual interface, or
return a special value to indicate the results of executing the tests.
Sample code :
------------------------------------------------Importing
Libraries------------------------------------
import unittest
from selenium import
webdriver
from
selenium.webdriver.common.keys import Keys
from
selenium.common.exceptions import TimeoutException
from pynput.keyboard
import Key, Controller
def
Keyboard_press_release():
keyboard.press(Key.ctrl)
keyboard.press(Key.tab)
keyboard.release(Key.ctrl)
keyboard.release(Key.tab)
------------------------------------------Main
Class --------------------------------------------------------------
class AdminPortalWorkflowTest(unittest.TestCase):
------------------------------------------Test
case 1 --------------------------------------------------------------
def test_Launch(self):
self.driver =
webdriver.Chrome('C:\Python27\Scripts\chromedriver.exe')
global keyboard
keyboard = Controller()
driver = self.driver
driver.get("https://www.google.com")
time.sleep(5)
Keyboard_press_release()
----------------------------------------Test
case 2 ----------------------------------------------------------------
def test_Check(self):
print "func starts ..."
----------------Created a
Suite which combines Testcase 1and 2 and returns a List -----------------
def suite():
suite = unittest.TestSuite()
suite.addTest(AdminPortalWorkflowTest('test_Launch'))
suite.addTest(AdminPortalWorkflowTest('test_Check'))
return suite
-----------------------------------------Main Function
------------------------------------------------------------
if __name__ == '__main__':
--------------------------------- Calling Suite which
runs all test --------------------------------------------
unittest.TextTestRunner(verbosity=2).run(suite())
Results :
No comments:
Post a Comment