Skip to content
icaton edited this page Jul 1, 2011 · 12 revisions

##Unit Test How To

Note: This section assumes some familiarity with the general concept of unit testing. For more information you can go to http://en.wikipedia.org/wiki/Unit_testing.

##Set up:

  1. You first need a directory with the in the same root directory as your sugarcrm directory. The name does not matter, in this example it is named ‘Test_’.

UT Test_

  1. Within this Directory will find ‘testSugarcrm.py’ this module handles logging in and logging out of the server before and after each test runs. First you must import ‘unittest’ and ‘sugarcrm’ (more on ‘sugarcrm’ later). The variables server, user and password must be changed to the url of the server you are logging into, the user name for that server and password for that user respectively.

UT Response

  1. All test modules must follow the naming convention ‘test*.py’ in order to be discovered. In this case * represents any string, for example testLogin.py fits the naming convention where * represents ‘Login’.

  2. In order for ‘testSugarcrm’ to be able to import the ‘sugarcrm’ module (which shares the root directory with ‘Test_’) you must create a link to it. This accomplished by changing directories to the ‘Test_‘ directory and executing this line of code in the command prompt.

ln –s ../sugarcrm sugarcrm

This creates a link to the ‘sugarcrm’ module within the ‘Test_’ directory that can be referred to as ‘sugarcrm’ (this is needed for ‘testSugarcrm’ to be able to import ‘sugarcrm’) .

UT Sugarcrm Test_ link

  1. The test modules must be set up in a specific manner in order to work. The following picture shows the important elements that must be included.

UT Important Elements

First from ‘testSugarcrm’ you must import ‘SugarcrmTest’ as well as ‘unittest’ These must be included exactly as pictured above on all test modules. You must define your test as a class that is passed ‘SugarcrmTest’. After that you may define any variables needed for the test and then run the test. At the end you must include the ‘if ‘ statement exactly as pictured above, this is needed for all test modules.

Now you can simply put your unittest files (e.g XXXtest.py)under that folder. Run /Tests/Sugar_Dynamic_Unittest.py, and it will automatically find all the unittest files under that folder and test them. You can view the Sugar_Dynamic_Unittest.py here.[Sugar_Dynamic_Unittest.py] (https://github.com/jmertic/KSU_Capstone_Spring_2011_Python/raw/master/Tests/Sugar_Dynamic_Unittest.py) The output of the Unittest should looks like this

![UT Dynamic] (https://github.com/sugarcrm/python_webservices_library/blob/master/Pictures/UT_Dynamic.jpg?raw=true)

OR Once you can drop it into the Tests directory and go to the command prompt and change directories to the Test directory. Next type the following command python –m unittest discover

This will discover and run all tests that follow the naming convention.