Presentation Unit Testing process


Published on

Published in: Technology, Business
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Presentation Unit Testing process

  1. 1. Unit Testing <ul><li>The primary goal of unit testing is </li></ul><ul><li>to take the smallest piece of testable software, </li></ul><ul><li>isolate it </li></ul><ul><li>from the remainder of the code, </li></ul><ul><li>and determine whether it behaves </li></ul><ul><li>exactly as you expect . </li></ul>by Bárbara Cabral Sofshore Software Engineering
  2. 2. Unit Testing <ul><li>What's important to know? </li></ul><ul><ul><li>The architecture of the system </li></ul></ul><ul><ul><li>How to work with testing frameworks </li></ul></ul><ul><ul><li>How to write good test-cases </li></ul></ul><ul><li>What is Good Unit Test? </li></ul><ul><ul><li>The test shall have a purpose to exist </li></ul></ul><ul><ul><li>The test shall prioritize only the main problems, it means, the tester shall think about the better way to test the code. </li></ul></ul>
  3. 3. TestNG <ul><li>TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities </li></ul><ul><li>Writing a test is typically a three-step process: </li></ul><ul><ul><li>Write the business logic of your test and insert TestNG annotations in your code. </li></ul></ul><ul><ul><li>Add the information about your test (e.g. the class name, the groups you wish to run, etc...) in a testng.xml file or in build.xml. </li></ul></ul><ul><ul><li>Run TestNG. </li></ul></ul>
  4. 4. TestNG <ul><li>package example1; </li></ul><ul><li>import org.testng.annotations.*; </li></ul><ul><li>public class SimpleTest { </li></ul><ul><ul><li>@BeforeClass </li></ul></ul><ul><ul><li>public void setUp() { </li></ul></ul><ul><ul><li>// code that will be invoked when this test is instantiated </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>@Test(groups = { &quot;fast&quot; }) </li></ul></ul><ul><ul><li>public void aFastTest() { </li></ul></ul><ul><ul><li>System.out.println(&quot;Fast test&quot;); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>@Test(groups = { &quot;slow&quot; }) </li></ul></ul><ul><ul><li>public void aSlowTest() { </li></ul></ul><ul><ul><li>System.out.println(&quot;Slow test&quot;); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  5. 5. testng.xml <ul><li><project default=&quot;test&quot;> </li></ul><ul><ul><li><taskdef name=&quot; testng &quot; classpathref=&quot;cp&quot; classname=&quot; org.testng.TestNGAntTask &quot; /> </li></ul></ul><ul><ul><li><target name=&quot;test&quot;> </li></ul></ul><ul><ul><li><testng classpathref=&quot;cp&quot; groups=&quot; fast &quot;> </li></ul></ul><ul><ul><li><classfileset dir=&quot;build&quot; includes=&quot;example1/*.class&quot;/> </li></ul></ul><ul><ul><li></testng> </li></ul></ul><ul><ul><li></target> </li></ul></ul><ul><li></project> </li></ul><ul><li><!DOCTYPE suite SYSTEM &quot;; > </li></ul><ul><li><suite name=&quot;Suite1&quot; verbose=&quot;1&quot; > </li></ul><ul><li><test name=&quot;Regression1&quot; > </li></ul><ul><ul><li><packages> </li></ul></ul><ul><ul><li> <package name=&quot;test.sample&quot; /> </li></ul></ul><ul><ul><li></packages> </li></ul></ul><ul><li></test> </li></ul><ul><li></suite> </li></ul>
  6. 6. DBUnit <ul><li>DbUnit is a JUnit extension (also usable with Ant) targeted at database-driven projects that, among other things, puts your database into a known state between test runs. </li></ul><ul><li>Best Pratices </li></ul><ul><ul><li>Use one database instance per developer </li></ul></ul><ul><ul><li>Good setup don't need cleanup! </li></ul></ul><ul><ul><li>Use multiple small datasets </li></ul></ul><ul><ul><li>Connection management strategies </li></ul></ul>
  7. 7. DBUnit <ul><li>Static data </li></ul><ul><ul><li>Testing can be simplified if you can get your database in a known state before a test is run. </li></ul></ul><ul><ul><li>For this, we have the sql files to build the database: tables, columns and static data (ex: Cities of the Belgium). </li></ul></ul><ul><ul><li>This kind of data rarely change! </li></ul></ul><ul><li>Dinamic data </li></ul><ul><ul><li>Most of your tests do not require the entire database to be re-initialized. So, instead of putting your entire database data in one large dataset, try to break it into many smaller chunks . </li></ul></ul><ul><ul><li>This reduces the overhead caused by initializing your database for each test. This also facilitates team development since many developers working on different components can modify datasets independently. </li></ul></ul><ul><ul><li>We can be have one dataSet to each class or to each test or to each group of tests. </li></ul></ul><ul><ul><li>This kind of data is not commited in the database! </li></ul></ul>
  8. 8. Strategy Run TestNG To create the static data To read the testng.xml file(s)
  9. 9. A very simple Unit Test <ul><li>class User { </li></ul><ul><li>private String name; </li></ul><ul><li>public String getName() { </li></ul><ul><li>return name; </li></ul><ul><li>} </li></ul><ul><li>public void setName(String newName) { </li></ul><ul><li>name = newName; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class User Test extends TestSuite { </li></ul><ul><li>public void testNameProperty() { </li></ul><ul><li>User user = new User(); </li></ul><ul><li>assertNull(user.getName()); </li></ul><ul><li>String testName = &quot;test&quot;; </li></ul><ul><li>user.setName(testName); </li></ul><ul><li>assertEquals(testName, user.getName()); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  10. 10. A very simple Good Unit Test <ul><li>public class TestUser { </li></ul><ul><li>Role userRole ; </li></ul><ul><li>Role developerRole ; </li></ul><ul><li>@BeforeClass </li></ul><ul><li>public void beforeClass() throws Exception { </li></ul><ul><ul><li>userRole = UtilTestUser. getRole (RoleType. USER ); </li></ul></ul><ul><ul><li>developerRole = UtilTestUser. getRole (RoleType. DEVELOPER ); </li></ul></ul><ul><li>} </li></ul><ul><li>/** </li></ul><ul><li>* 1. To create an user object </li></ul><ul><li>* 2. Add the role(enum) to the role list of the user object </li></ul><ul><li>* 3. Check if the role really was added </li></ul><ul><li>* @throws InvalidNameException </li></ul><ul><li>*/ </li></ul><ul><li>@Test (groups = { &quot;API&quot; }) </li></ul><ul><li>public void testAddRole() throws InvalidNameException { </li></ul><ul><ul><li>User user = UserFactory. createUser ( &quot;asen&quot; , &quot;as12$as&quot; , &quot;Airton Senna&quot; ); </li></ul></ul><ul><ul><li>user.addRole( userRole ); </li></ul></ul><ul><ul><li>Assert. assertNotNull (user.getRoles()); </li></ul></ul><ul><ul><li>Assert. assertEquals (user.getRoles().size(), 1); </li></ul></ul><ul><ul><li>Assert. assertEquals (user.getRoles().iterator().next(), userRole ); </li></ul></ul><ul><li>} </li></ul>Test Script
  11. 11. Example: The Architecture of the User Management <ul><ul><li>User <Interface> </li></ul></ul><ul><ul><ul><li>The contract to manipulate the object User </li></ul></ul></ul><ul><ul><ul><li>We need generate a TestUser </li></ul></ul></ul><ul><ul><li>UserDAO <Interface> </li></ul></ul><ul><ul><ul><li>The contract to manipulate the object UserHibernateDAO </li></ul></ul></ul><ul><ul><ul><li>The object UserHibernateDAO is responsible to provide a persistence layer to connect the object with the database </li></ul></ul></ul><ul><ul><li>UserManager <Interface> </li></ul></ul><ul><ul><ul><li>The contract to manipulate the object DefaultUserManager </li></ul></ul></ul><ul><ul><ul><li>The object DefaultUserManager is responsible to provide some services as to load an user by its logon identifier and to save a new user or to save the object user updated. </li></ul></ul></ul>
  12. 12. TestUserManager <ul><li>public class TestUserManager extends DBUnitTestCase { </li></ul><ul><li>UserManager userManager ; </li></ul><ul><li>@BeforeClass </li></ul><ul><li>public void beforeClass() throws Exception { </li></ul><ul><li>userManager = UtilTestUser. getTestUserManager (); </li></ul><ul><li>} </li></ul><ul><li>/** </li></ul><ul><li>* 1. A new user with an existent userId is created. </li></ul><ul><li>* 2. The user is saved </li></ul><ul><li>* 3. A UserIdExistsException is thrown </li></ul><ul><li>*/ </li></ul><ul><li>@Test (groups = { &quot;API&quot; }, expectedExceptions = { UserAlreadyExistsException. class }) </li></ul><ul><li>public void saveUserWithTheSameUserIdCausesException() </li></ul><ul><ul><li>throws InvalidNameException, UserAlreadyExistsException, DataAccessException, ServiceException { </li></ul></ul><ul><ul><li>User user1 = UserFactory. createUser ( &quot;gkue&quot; , &quot;as12$as&quot; , &quot;Gustavo Kuerten&quot; ); </li></ul></ul><ul><ul><li>userManager .saveUser(user1); </li></ul></ul><ul><ul><li>User user2 = UserFactory. createUser ( &quot;gkue&quot; , &quot;as12$as&quot; , &quot;Gustavo Kuerten&quot; ); </li></ul></ul><ul><ul><li>userManager .saveUser(user2); </li></ul></ul><ul><li>} </li></ul>
  13. 13. The database unit test <ul><li>public class TestUser DAO extends DBUnitTestCase { </li></ul><ul><li>private static final String[] TABLES_CHECK_USER = { &quot;users&quot; , &quot;roles&quot; , &quot;user_roles&quot; }; </li></ul><ul><li>//(...) --> a configuration code here </li></ul><ul><li>/** </li></ul><ul><li>* 1.The object user shall be created with all attributes and roles </li></ul><ul><li>* 2.The object shall be saved in the database </li></ul><ul><li>* 3.The dataSet expected shall be created to compare against the user object stored </li></ul><ul><li>*/ </li></ul><ul><li>@Test (groups={ &quot;database&quot; }) </li></ul><ul><li>public void testSaveUser() throws InvalidNameException, DatabaseUnitException, SQLException, IOException, UserAlreadyExistsException, DAOException, ServiceException { </li></ul><ul><ul><li>usesDataset( DATASET_BASE ); </li></ul></ul><ul><ul><li>User user = UserFactory. createUser ( &quot;pcoe&quot; , &quot;pc910*pc&quot; , &quot;Paulo Coelho&quot; ); </li></ul></ul><ul><ul><li>user.addRole( userRole ); </li></ul></ul><ul><ul><li>user.addRole( adminRole ); </li></ul></ul><ul><ul><li>userDao .saveUser(user); </li></ul></ul><ul><ul><li>assertDataSet( &quot;saveUserExpected&quot; , TABLES_CHECK_USER ); </li></ul></ul><ul><li>} </li></ul>