Unit Testing - Nakov's Talk @ VarnaConf 2013

20
Unit Testing Building Rock-Solid Software Svetlin Nakov Telerik Software Academy http://academy.telerik.com Manager Technical Training http://www.nakov.com

description

Unit testing - презентацията на Наков на ВарнаКонф 2013 - http://www.varnaconf.com

Transcript of Unit Testing - Nakov's Talk @ VarnaConf 2013

Page 1: Unit Testing - Nakov's Talk @ VarnaConf 2013

Unit TestingBuilding Rock-Solid Software

Svetlin Nakov

Telerik Software Academyhttp://academy.telerik.com

Manager Technical Traininghttp://www.nakov.com

Page 2: Unit Testing - Nakov's Talk @ VarnaConf 2013

What is Unit Testing?

Page 3: Unit Testing - Nakov's Talk @ VarnaConf 2013

Unit Test – Definition

A unit test is a piece of code written by a developer that exercises a very small, specific area of functionality of the code being tested.

“Program testing can be used to show the presence of bugs, but never to show their absence!”

Edsger Dijkstra, [1972]

Page 4: Unit Testing - Nakov's Talk @ VarnaConf 2013

Unit Test – Example

int sum(int[] array) { int sum = 0; for (int i=0; i<array.length; i++) sum += array[i]; return sum;}

void testSum() { if (sum(new int[]{1,2}) != 3) throw new TestFailedException("1+2 != 3"); if (sum(new int[]{-2}) != -2) throw new TestFailedException("-2 != -2"); if (sum(new int[]{}) != 0) throw new TestFailedException("0 != 0");}

Page 5: Unit Testing - Nakov's Talk @ VarnaConf 2013

Unit Testing – Some Facts

Tests are pieces of code (small programs)

In most cases unit tests are written by developers, not by QA engineers

Unit tests are released into the code repository (TFS / SVN / Git) along with the code they test

Unit testing framework is needed

Visual Studio Team Test (VSTT)

NUnit, MbUnit, Gallio, etc.5

Page 6: Unit Testing - Nakov's Talk @ VarnaConf 2013

Unit Testing Frameworks &

JUnit

Page 7: Unit Testing - Nakov's Talk @ VarnaConf 2013

Unit Testing Frameworks

JUnit The first popular unit testing

framework

Based on Java, written by Kent Beck & Co.

Similar frameworks have been developed for a broad range of computer languages NUnit – for C# and all .NET

languages

cppUnit, jsUnit, PhpUnit, PerlUnit, ...

Visual Studio Team Test (VSTT) Developed by Microsoft, integrated

in VS

7

Page 8: Unit Testing - Nakov's Talk @ VarnaConf 2013

Unit Testing Frameworks

JUnit – the first unit testing framework www.junit.org

Based on Java

Developed by Erich Gamma and Kent Beck

Unit testing frameworks have been developed for a broad range of computer languages NUnit – for C#, VB.NET and .NET

languages

cppUnit, DUnit, jsUnit, PhpUnit, PerlUnit, ...

List of xUnit frameworks can be found at: http://www.testingfaqs.org/t-unit.html

Page 9: Unit Testing - Nakov's Talk @ VarnaConf 2013

JUnit – Features

Test code is annotated using Java 5 annotations

Test code contains assertions Tests are grouped in test fixtures and test suites

Several execution interfaces Eclipse integrated plug-in Console interface:

java org.junit.runner.JUnitCore <test-class>

Page 10: Unit Testing - Nakov's Talk @ VarnaConf 2013

JUnit – Annotations @Test

Annotates a test case method @Before, @After

Annotates methods called before/after each test case

@BeforeClass, @AfterClass Annotates methods called one time

before and after all test cases in the class

@Ignore Ignores a test case 10

Page 11: Unit Testing - Nakov's Talk @ VarnaConf 2013

JUnit – Assertions Using org.junit.Assert class

Assertions check a condition and throw exception if the condition is not satisfied Comparing values

assertEquals ([message], expected value, calculated value) Comparing objects

assertNull([message], object) assertNotNull([message], object) assertSame ([message], expected obj, calculated obj)

Page 12: Unit Testing - Nakov's Talk @ VarnaConf 2013

JUnit – Assertions (2) Conditions

assertTrue(condition)

assertFalse(condition)

Forced test fail

fail()

Expecting exception

@Test(expected=IndexOutOfBoundsException.class)

Page 13: Unit Testing - Nakov's Talk @ VarnaConf 2013

Java class that needs unit testing:

JUnit – Example

public class Sumator {

public int sum(int a, int b) { int sum = a + b; return sum; }

public int sum(int... nums) { int sum = 0; for (int i = 0; i < nums; i++) sum += nums[i]; return sum; }

}

Page 14: Unit Testing - Nakov's Talk @ VarnaConf 2013

JUnit based test class (fixture):

JUnit – Example

import org.junit.Test;import static org.junit.Assert.*;

public class SumatorTest {

@Test public void testSum() { Sumator sumator = new Sumator(); int sum = sumator.sum(new int[] {2,3,4}); assertEquals(sum, 9); }

}

Page 15: Unit Testing - Nakov's Talk @ VarnaConf 2013

JUnit text fixture with setup and cleanup:

JUnit – Example

public class SumatorTest { private Sumator sumator; @Before public void setUpTestCase() { this.sumator = new Sumator(); } @Test public void testSum() { int sum = sumator.sum(2, 3); assertEquals(sum, 5); } @After public void cleanUpTestCase() { this.sumator = null; }}

Page 16: Unit Testing - Nakov's Talk @ VarnaConf 2013

Test Suites

Suits are sets of JUnit test classes We can define test suits with annotations:import org.junit.runner.RunWith;import org.junit.runners.Suite;import org.junit.runners.Suite.SuiteClasses;

@RunWith(value=Suite.class)@SuiteClasses(value={Test1.class, Test2.class})public class AllTests {

}

Page 17: Unit Testing - Nakov's Talk @ VarnaConf 2013

Eclipse and JUnit

Eclipse has built-in support for integration with JUnit

Can create, run and debug JUnit tests

Page 18: Unit Testing - Nakov's Talk @ VarnaConf 2013

Testing with JUnitLive Demo

Page 19: Unit Testing - Nakov's Talk @ VarnaConf 2013

форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно

програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки

уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop

уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC

безплатен курс "Разработка на софтуер в cloud среда"

BG Coder - онлайн състезателна система - online judge

курсове и уроци по програмиране, книги – безплатно от Наков

безплатен курс "Качествен програмен код"

алго академия – състезателно програмиране, състезания

ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NETкурсове и уроци по програмиране – Телерик академия

курс мобилни приложения с iPhone, Android, WP7, PhoneGap

free C# book, безплатна книга C#, книга Java, книга C#Дончо Минков - сайт за програмиранеНиколай Костов - блог за програмиранеC# курс, програмиране, безплатно

?

? ? ??

?? ?

?

?

?

??

?

?

? ?

Questions?

?

Unit Testing

http://academy.telerik.com

Page 20: Unit Testing - Nakov's Talk @ VarnaConf 2013

Free Trainings @ Telerik Academy

C# Programming @ Telerik Academy csharpfundamentals.telerik.com

Telerik Software Academy academy.telerik.com

Telerik Academy @ Facebook facebook.com/TelerikAcademy

Telerik Software Academy Forums forums.academy.telerik.com