View source | Discuss this page | Page history | Printable version   

ERP 2.50:Developers Guide/How to create testcases/it

Contents

Obiettivo

This how-to will focus on creating a testcase making use of the Openbravo test base classes. The testcase will check that our system has at least one User with password. For this, we'll use the new DAL approach to access the database.

In computer programming, unit testing is a software design and development method where the programmer gains confidence that individual units of source code are fit for use. A unit is the smallest testable part of an application. In procedural programming a unit may be an individual program, function, procedure, etc., while in object-oriented programming, the smallest unit is a method, which may belong to a base/super class, abstract class or derived/child class.[1]

All new developments must belong to a module that is not the core module. Please follow the How to create and package a module section to create a new module.

Creare i testcase

Per iniziare

Testcases del Core di Openbravo ERP

Openbravo ERP provides a set of unit tests, often called 'test cases' for things such as: runtime model consistency, data access layer features, webservices features, etc. This test cases are based on JUnit testing framework.

The set of tests you can find and study them in the src-test folder under your source root folder:

root_source
 |- build
 |- config
 |- legal
 |- lib
 |- modules
 |- referencedata
 |- src
 |- srcAD
 |- srcClient
 |- src-core
 |- src-db
 |- src-diagnostics
 |- src-gen
 |- src-test            <-- source of the core testcases
 |- src-trl
 |- src-wad
 |- web
 |- WebContent

Here is a direct link to the testcases in the code repository: Openbravo 2.50 test cases.

Ereditare da BaseTest

All the core testcases are subclasses from BaseTest (org.openbravo.test.base.BaseTest). This class handles all the necessary steps to initialize the Data Access Layer, takes care of transaction handling and provides a set of utilities (methods) for working with the Openbravo context (OBContext).

Creare la classe Java

 
 package org.openbravo.test.examples;
 
 import java.util.List;
 
 import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.model.ad.access.User;
 import org.openbravo.test.base.BaseTest;
 
 public class ExampleTest extends BaseTest {
 
   public void testUsersCount() {
     setSystemAdministratorContext();
     final OBCriteria<User> uCriteria = OBDal.getInstance().createCriteria(User.class);
     final List<User> uList = uCriteria.list();
     int userCount = 0;
     for (User u : uList) {
       if (u.getPassword().length() > 0)
        userCount++;
     }
     assertTrue(userCount > 0);
     System.out.println("Total of users with password: " + (userCount));
   }
 }
 

Comprendere la classe

You have just created a new class named Example that extends from the BaseTest class.

 
public void testUsersCount() {}

This class has a testUsersCount function. Note that all testing methods must start with test in the function name. e.g. testAllWarehouses(), testMyFirstTest(), etc

 
setSystemAdministratorContext();

Sets the context as if a System Administrator is logged in the application.

Bulbgraph.png   There is a setUserContext(...) method to select running as another user, this method can be called in the middle of a testcase to switch user (if required)
 
final OBCriteria<User> uCriteria = OBDal.getInstance().createCriteria(User.class);
final List<User> uList = uCriteria.list();

Uses the OBDal instance to create a new OBCriteria object, and uses it for listing all (since we are not filtering) the Users in the database.

 
int userCount = 0;
 for (User u : uList) {
   if (u.getPassword().length() > 0)
     userCount++;
 }

We loop trough the uList collection, and we increment the userCount variable if the user has a password length > 0.

 
assertTrue(userCount > 0);

We assert that the userCount is more than 0. You can check the list of possible assertions in JUnit's Assert class API.

 
 System.out.println("Total of users with password: " + (userCount));

Finally we print a total of users with password just for the record.

Transaction Handling

A question which might pop-up when looking at the above code: where is the database transaction handling done? The answer is that this is handled by the BaseTest class and the Openbravo data access layer:

The BaseTest class detects automatically if an exception happened or not.

There are certainly cases whereby it makes sense to have more control over the database transactions. There are a number of relevant methods which can be useful then:

Side-Effect Free

A test case will often change the data in the underlying database. Most of the time it is not feasible to setup a completely new test database for each test run. Therefore test-cases should be developed such that they are side effect free. This means:

This last point is important because there can be always reasons why during a test the clean-up step is not performed. For example because the test run is stopped before the clean-up is done.

Ant Test Tasks

Openbravo has a number of ant tasks which run the test cases:

All the test cases are based on the Small Bazaar default data.

When adding new test classes to Openbravo ERP the developer has to always add the test class to the AllAntTaskTests test suite and if it is side effect free and quick to the AllQuickAntTaskTests and if it is side effect free but takes a bit more time to the AntTaskTests test suite.

Il Risultato

To be able to execute your testcases:

Testcases1.png
Testcases2.png
Testcases3.png

Note

  1. http://en.wikipedia.org/wiki/Unit_testing

Retrieved from "http://wiki.openbravo.com/wiki/ERP_2.50:Developers_Guide/How_to_create_testcases/it"

This page has been accessed 4,358 times. This page was last modified on 14 June 2011, at 11:04. Content is available under Creative Commons Attribution-ShareAlike 2.5 Spain License.