ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
UnitTesting
Building Rock-Solid Software
Svetlin Nakov
Telerik Software Academy
http://academy.telerik.com
ManagerTechnicalTraining
http://www.nakov.com
What is UnitTesting?
UnitTest ¨C 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]
UnitTest ¨C 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");
}
UnitTesting ¨C 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 StudioTeamTest (VSTT)
? NUnit, MbUnit, Gallio, etc.
5
UnitTesting
Frameworks & JUnit
UnitTesting 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 ¨C for C# and all .NET languages
? cppUnit, jsUnit, PhpUnit, PerlUnit, ...
? Visual StudioTeamTest (VSTT)
? Developed by Microsoft, integrated inVS
7
UnitTesting Frameworks
? JUnit ¨C 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 ¨C 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
JUnit ¨C 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>
JUnit ¨C 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
JUnit ¨C 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)
JUnit ¨C Assertions (2)
? Conditions
? assertTrue(condition)
? assertFalse(condition)
? Forced test fail
? fail()
? Expecting exception
? @Test(expected=IndexOutOfBoundsExcept
ion.class)
? Java class that needs unit testing:
JUnit ¨C 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;
}
}
? JUnit based test class (fixture):
JUnit ¨C 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);
}
}
? JUnit text fixture with setup and cleanup:
JUnit ¨C 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;
}
}
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 {
}
Eclipse and JUnit
? Eclipse has
built-in
support for
integration
with JUnit
? Can create,
run and
debug JUnit
tests
Testing with JUnit
Live Demo
§æ§à§â§å§Þ§á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö,§æ§à§â§å§Þ §å§Ö§Ò §Õ§Ú§Ù§Ñ§Û§ß
§Ü§å§â§ã§à§Ó§Ö§Ú §å§â§à§è§Ú §á§à §á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö,§å§Ö§Ò §Õ§Ú§Ù§Ñ§Û§ß ¨C §Ò§Ö§Ù§á§Ý§Ñ§ä§ß§à
§á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö§Ù§Ñ §Õ§Ö§è§Ñ ¨C §Ò§Ö§Ù§á§Ý§Ñ§ä§ß§Ú §Ü§å§â§ã§à§Ó§Ö §Ú §å§â§à§è§Ú
§Ò§Ö§Ù§á§Ý§Ñ§ä§Ö§ßSEO §Ü§å§â§ã -§à§á§ä§Ú§Þ§Ú§Ù§Ñ§è§Ú§ñ §Ù§Ñ §ä§ì§â§ã§Ñ§é§Ü§Ú
§å§â§à§è§Ú§á§à §å§Ö§Ò §Õ§Ú§Ù§Ñ§Û§ß, HTML,CSS, JavaScript,Photoshop
§å§â§à§è§Ú§á§à §á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö§Ú §å§Ö§Ò §Õ§Ú§Ù§Ñ§Û§ß §Ù§Ñ §å§é§Ö§ß§Ú§è§Ú
ASP.NETMVC§Ü§å§â§ã ¨C HTML,SQL,C#,.NET,ASP.NETMVC
§Ò§Ö§Ù§á§Ý§Ñ§ä§Ö§ß§Ü§å§â§ã"§²§Ñ§Ù§â§Ñ§Ò§à§ä§Ü§Ñ §ß§Ñ §ã§à§æ§ä§å§Ö§â §Ó cloud §ã§â§Ö§Õ§Ñ"
BGCoder -§à§ß§Ý§Ñ§Û§ß §ã§ì§ã§ä§Ö§Ù§Ñ§ä§Ö§Ý§ß§Ñ §ã§Ú§ã§ä§Ö§Þ§Ñ -online judge
§Ü§å§â§ã§à§Ó§Ö§Ú §å§â§à§è§Ú §á§à §á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö,§Ü§ß§Ú§Ô§Ú ¨C §Ò§Ö§Ù§á§Ý§Ñ§ä§ß§à §à§ä§¯§Ñ§Ü§à§Ó
§Ò§Ö§Ù§á§Ý§Ñ§ä§Ö§ß§Ü§å§â§ã"§¬§Ñ§é§Ö§ã§ä§Ó§Ö§ß §á§â§à§Ô§â§Ñ§Þ§Ö§ß§Ü§à§Õ"
§Ñ§Ý§Ô§à§Ñ§Ü§Ñ§Õ§Ö§Þ§Ú§ñ ¨C §ã§ì§ã§ä§Ö§Ù§Ñ§ä§Ö§Ý§ß§à §á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö,§ã§ì§ã§ä§Ö§Ù§Ñ§ß§Ú§ñ
ASP.NET§Ü§å§â§ã -§å§Ö§Ò §á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö,§Ò§Ñ§Ù§Ú §Õ§Ñ§ß§ß§Ú, C#,.NET,ASP.NET
§Ü§å§â§ã§à§Ó§Ö§Ú §å§â§à§è§Ú §á§à §á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö¨C §´§Ö§Ý§Ö§â§Ú§Ü §Ñ§Ü§Ñ§Õ§Ö§Þ§Ú§ñ
§Ü§å§â§ã§Þ§à§Ò§Ú§Ý§ß§Ú §á§â§Ú§Ý§à§Ø§Ö§ß§Ú§ñ §ã iPhone, Android,WP7,PhoneGap
freeC#book, §Ò§Ö§Ù§á§Ý§Ñ§ä§ß§Ñ §Ü§ß§Ú§Ô§Ñ C#,§Ü§ß§Ú§Ô§Ñ Java,§Ü§ß§Ú§Ô§Ñ C#
§¥§à§ß§é§à §®§Ú§ß§Ü§à§Ó -§ã§Ñ§Û§ä §Ù§Ñ §á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö
§¯§Ú§Ü§à§Ý§Ñ§Û §¬§à§ã§ä§à§Ó -§Ò§Ý§à§Ô §Ù§Ñ §á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö
C#§Ü§å§â§ã,§á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö,§Ò§Ö§Ù§á§Ý§Ñ§ä§ß§à
UnitTesting
http://academy.telerik.com
FreeTrainings @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

More Related Content

What's hot (20)

Unit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran JanardhanaUnit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran Janardhana
Ravikiran J
?
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
Pokpitch Patcharadamrongkul
?
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
pallavikhandekar212
?
What is JUnit? | Edureka
What is JUnit? | EdurekaWhat is JUnit? | Edureka
What is JUnit? | Edureka
Edureka!
?
testng
testngtestng
testng
harithakannan
?
Junit
JunitJunit
Junit
Manav Prasad
?
Junit
JunitJunit
Junit
Abdullah Shahneel
?
TestNG vs JUnit: cease fire or the end of the war
TestNG vs JUnit: cease fire or the end of the warTestNG vs JUnit: cease fire or the end of the war
TestNG vs JUnit: cease fire or the end of the war
Oleksiy Rezchykov
?
xUnit Style Database Testing
xUnit Style Database TestingxUnit Style Database Testing
xUnit Style Database Testing
Chris Oldwood
?
JUnit 4
JUnit 4JUnit 4
JUnit 4
Sunil OS
?
C++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkC++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing Framework
Humberto Marchezi
?
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
Nayanda Haberty
?
TestNG vs Junit
TestNG vs JunitTestNG vs Junit
TestNG vs Junit
B¨¹?ra ???z
?
Unit testing
Unit testingUnit testing
Unit testing
NexThoughts Technologies
?
Introduction To J unit
Introduction To J unitIntroduction To J unit
Introduction To J unit
Olga Extone
?
TestNG vs. JUnit4
TestNG vs. JUnit4TestNG vs. JUnit4
TestNG vs. JUnit4
Andrey Oleynik
?
TestNG introduction
TestNG introductionTestNG introduction
TestNG introduction
Denis Bazhin
?
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentation
Sanjib Dhar
?
Junit
JunitJunit
Junit
FAROOK Samath
?
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
Devvrat Shukla
?

Similar to Unit testing by Svetlin Nakov (20)

J unit presentation
J unit presentationJ unit presentation
J unit presentation
Priya Sharma
?
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
priya_trivedi
?
8-testing.pptx
8-testing.pptx8-testing.pptx
8-testing.pptx
ssuserd0fdaa
?
Test ng tutorial
Test ng tutorialTest ng tutorial
Test ng tutorial
Srikrishna k
?
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
Ahmed M. Gomaa
?
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Anand Kumar Rajana
?
Unit test
Unit testUnit test
Unit test
????? ?????? ???? ? ?? ??? ???(Cloud & Mobile Systems Lab @ SNU)
?
3 j unit
3 j unit3 j unit
3 j unit
kishoregali
?
Session 4 -Junit- Testing Exceptions, Junit hooks.pptx
Session 4 -Junit- Testing Exceptions, Junit hooks.pptxSession 4 -Junit- Testing Exceptions, Junit hooks.pptx
Session 4 -Junit- Testing Exceptions, Junit hooks.pptx
trainingdecorpo
?
31b - JUnit and Mockito.pdf
31b - JUnit and Mockito.pdf31b - JUnit and Mockito.pdf
31b - JUnit and Mockito.pdf
gauravavam
?
Junit 5 - Maior e melhor
Junit 5 - Maior e melhorJunit 5 - Maior e melhor
Junit 5 - Maior e melhor
Tiago de Freitas Lima
?
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptx
Knoldus Inc.
?
Security Testing
Security TestingSecurity Testing
Security Testing
Kiran Kumar
?
Testing with Junit4
Testing with Junit4Testing with Junit4
Testing with Junit4
Amila Paranawithana
?
TestNG Framework
TestNG Framework TestNG Framework
TestNG Framework
Levon Apreyan
?
Junit With Eclipse
Junit With EclipseJunit With Eclipse
Junit With Eclipse
Sunil kumar Mohanty
?
Junit and cactus
Junit and cactusJunit and cactus
Junit and cactus
Himanshu
?
Testes? Mas isso n?o aumenta o tempo de projecto? N?o quero...
Testes? Mas isso n?o aumenta o tempo de projecto? N?o quero...Testes? Mas isso n?o aumenta o tempo de projecto? N?o quero...
Testes? Mas isso n?o aumenta o tempo de projecto? N?o quero...
Comunidade NetPonto
?
SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)
Amr E. Mohamed
?
Python unit testing
Python unit testingPython unit testing
Python unit testing
Darryl Sherman
?

Recently uploaded (20)

DAO UTokyo 2025 DLT mass adoption case studies IBM Tsuyoshi Hirayama (ƽɽÒã)
DAO UTokyo 2025 DLT mass adoption case studies IBM Tsuyoshi Hirayama (ƽɽÒã)DAO UTokyo 2025 DLT mass adoption case studies IBM Tsuyoshi Hirayama (ƽɽÒã)
DAO UTokyo 2025 DLT mass adoption case studies IBM Tsuyoshi Hirayama (ƽɽÒã)
Tsuyoshi Hirayama
?
Brave Browser Crack 1.45.133 Activated 2025
Brave Browser Crack 1.45.133 Activated 2025Brave Browser Crack 1.45.133 Activated 2025
Brave Browser Crack 1.45.133 Activated 2025
kherorpacca00126
?
Gojek Clone Multi-Service Super App.pptx
Gojek Clone Multi-Service Super App.pptxGojek Clone Multi-Service Super App.pptx
Gojek Clone Multi-Service Super App.pptx
V3cube
?
Integrated Operating Window - A Gateway to PM
Integrated Operating Window - A Gateway to PMIntegrated Operating Window - A Gateway to PM
Integrated Operating Window - A Gateway to PM
Farhan Tariq
?
Q4_TLE-7-Lesson-6-Week-6.pptx 4th quarter
Q4_TLE-7-Lesson-6-Week-6.pptx 4th quarterQ4_TLE-7-Lesson-6-Week-6.pptx 4th quarter
Q4_TLE-7-Lesson-6-Week-6.pptx 4th quarter
MariaBarbaraPaglinaw
?
Early Adopter's Guide to AI Moderation (Preview)
Early Adopter's Guide to AI Moderation (Preview)Early Adopter's Guide to AI Moderation (Preview)
Early Adopter's Guide to AI Moderation (Preview)
nick896721
?
Unlocking DevOps Secuirty :Vault & Keylock
Unlocking DevOps Secuirty :Vault & KeylockUnlocking DevOps Secuirty :Vault & Keylock
Unlocking DevOps Secuirty :Vault & Keylock
HusseinMalikMammadli
?
Unlock AI Creativity: Image Generation with DALL¡¤E
Unlock AI Creativity: Image Generation with DALL¡¤EUnlock AI Creativity: Image Generation with DALL¡¤E
Unlock AI Creativity: Image Generation with DALL¡¤E
Expeed Software
?
Technology use over time and its impact on consumers and businesses.pptx
Technology use over time and its impact on consumers and businesses.pptxTechnology use over time and its impact on consumers and businesses.pptx
Technology use over time and its impact on consumers and businesses.pptx
kaylagaze
?
Field Device Management Market Report 2030 - TechSci Research
Field Device Management Market Report 2030 - TechSci ResearchField Device Management Market Report 2030 - TechSci Research
Field Device Management Market Report 2030 - TechSci Research
Vipin Mishra
?
A Framework for Model-Driven Digital Twin Engineering
A Framework for Model-Driven Digital Twin EngineeringA Framework for Model-Driven Digital Twin Engineering
A Framework for Model-Driven Digital Twin Engineering
Daniel Lehner
?
DevNexus - Building 10x Development Organizations.pdf
DevNexus - Building 10x Development Organizations.pdfDevNexus - Building 10x Development Organizations.pdf
DevNexus - Building 10x Development Organizations.pdf
Justin Reock
?
Replacing RocksDB with ScyllaDB in Kafka Streams by Almog Gavra
Replacing RocksDB with ScyllaDB in Kafka Streams by Almog GavraReplacing RocksDB with ScyllaDB in Kafka Streams by Almog Gavra
Replacing RocksDB with ScyllaDB in Kafka Streams by Almog Gavra
ScyllaDB
?
Formal Methods: Whence and Whither? [Martin Fr?nzle Festkolloquium, 2025]
Formal Methods: Whence and Whither? [Martin Fr?nzle Festkolloquium, 2025]Formal Methods: Whence and Whither? [Martin Fr?nzle Festkolloquium, 2025]
Formal Methods: Whence and Whither? [Martin Fr?nzle Festkolloquium, 2025]
Jonathan Bowen
?
Revolutionizing-Government-Communication-The-OSWAN-Success-Story
Revolutionizing-Government-Communication-The-OSWAN-Success-StoryRevolutionizing-Government-Communication-The-OSWAN-Success-Story
Revolutionizing-Government-Communication-The-OSWAN-Success-Story
ssuser52ad5e
?
EaseUS Partition Master Crack 2025 + Serial Key
EaseUS Partition Master Crack 2025 + Serial KeyEaseUS Partition Master Crack 2025 + Serial Key
EaseUS Partition Master Crack 2025 + Serial Key
kherorpacca127
?
Endpoint Backup: 3 Reasons MSPs Ignore It
Endpoint Backup: 3 Reasons MSPs Ignore ItEndpoint Backup: 3 Reasons MSPs Ignore It
Endpoint Backup: 3 Reasons MSPs Ignore It
MSP360
?
Build with AI on Google Cloud Session #4
Build with AI on Google Cloud Session #4Build with AI on Google Cloud Session #4
Build with AI on Google Cloud Session #4
Margaret Maynard-Reid
?
Q4 2024 Earnings and Investor Presentation
Q4 2024 Earnings and Investor PresentationQ4 2024 Earnings and Investor Presentation
Q4 2024 Earnings and Investor Presentation
Dropbox
?
MIND Revenue Release Quarter 4 2024 - Finacial Presentation
MIND Revenue Release Quarter 4 2024 - Finacial PresentationMIND Revenue Release Quarter 4 2024 - Finacial Presentation
MIND Revenue Release Quarter 4 2024 - Finacial Presentation
MIND CTI
?
DAO UTokyo 2025 DLT mass adoption case studies IBM Tsuyoshi Hirayama (ƽɽÒã)
DAO UTokyo 2025 DLT mass adoption case studies IBM Tsuyoshi Hirayama (ƽɽÒã)DAO UTokyo 2025 DLT mass adoption case studies IBM Tsuyoshi Hirayama (ƽɽÒã)
DAO UTokyo 2025 DLT mass adoption case studies IBM Tsuyoshi Hirayama (ƽɽÒã)
Tsuyoshi Hirayama
?
Brave Browser Crack 1.45.133 Activated 2025
Brave Browser Crack 1.45.133 Activated 2025Brave Browser Crack 1.45.133 Activated 2025
Brave Browser Crack 1.45.133 Activated 2025
kherorpacca00126
?
Gojek Clone Multi-Service Super App.pptx
Gojek Clone Multi-Service Super App.pptxGojek Clone Multi-Service Super App.pptx
Gojek Clone Multi-Service Super App.pptx
V3cube
?
Integrated Operating Window - A Gateway to PM
Integrated Operating Window - A Gateway to PMIntegrated Operating Window - A Gateway to PM
Integrated Operating Window - A Gateway to PM
Farhan Tariq
?
Q4_TLE-7-Lesson-6-Week-6.pptx 4th quarter
Q4_TLE-7-Lesson-6-Week-6.pptx 4th quarterQ4_TLE-7-Lesson-6-Week-6.pptx 4th quarter
Q4_TLE-7-Lesson-6-Week-6.pptx 4th quarter
MariaBarbaraPaglinaw
?
Early Adopter's Guide to AI Moderation (Preview)
Early Adopter's Guide to AI Moderation (Preview)Early Adopter's Guide to AI Moderation (Preview)
Early Adopter's Guide to AI Moderation (Preview)
nick896721
?
Unlocking DevOps Secuirty :Vault & Keylock
Unlocking DevOps Secuirty :Vault & KeylockUnlocking DevOps Secuirty :Vault & Keylock
Unlocking DevOps Secuirty :Vault & Keylock
HusseinMalikMammadli
?
Unlock AI Creativity: Image Generation with DALL¡¤E
Unlock AI Creativity: Image Generation with DALL¡¤EUnlock AI Creativity: Image Generation with DALL¡¤E
Unlock AI Creativity: Image Generation with DALL¡¤E
Expeed Software
?
Technology use over time and its impact on consumers and businesses.pptx
Technology use over time and its impact on consumers and businesses.pptxTechnology use over time and its impact on consumers and businesses.pptx
Technology use over time and its impact on consumers and businesses.pptx
kaylagaze
?
Field Device Management Market Report 2030 - TechSci Research
Field Device Management Market Report 2030 - TechSci ResearchField Device Management Market Report 2030 - TechSci Research
Field Device Management Market Report 2030 - TechSci Research
Vipin Mishra
?
A Framework for Model-Driven Digital Twin Engineering
A Framework for Model-Driven Digital Twin EngineeringA Framework for Model-Driven Digital Twin Engineering
A Framework for Model-Driven Digital Twin Engineering
Daniel Lehner
?
DevNexus - Building 10x Development Organizations.pdf
DevNexus - Building 10x Development Organizations.pdfDevNexus - Building 10x Development Organizations.pdf
DevNexus - Building 10x Development Organizations.pdf
Justin Reock
?
Replacing RocksDB with ScyllaDB in Kafka Streams by Almog Gavra
Replacing RocksDB with ScyllaDB in Kafka Streams by Almog GavraReplacing RocksDB with ScyllaDB in Kafka Streams by Almog Gavra
Replacing RocksDB with ScyllaDB in Kafka Streams by Almog Gavra
ScyllaDB
?
Formal Methods: Whence and Whither? [Martin Fr?nzle Festkolloquium, 2025]
Formal Methods: Whence and Whither? [Martin Fr?nzle Festkolloquium, 2025]Formal Methods: Whence and Whither? [Martin Fr?nzle Festkolloquium, 2025]
Formal Methods: Whence and Whither? [Martin Fr?nzle Festkolloquium, 2025]
Jonathan Bowen
?
Revolutionizing-Government-Communication-The-OSWAN-Success-Story
Revolutionizing-Government-Communication-The-OSWAN-Success-StoryRevolutionizing-Government-Communication-The-OSWAN-Success-Story
Revolutionizing-Government-Communication-The-OSWAN-Success-Story
ssuser52ad5e
?
EaseUS Partition Master Crack 2025 + Serial Key
EaseUS Partition Master Crack 2025 + Serial KeyEaseUS Partition Master Crack 2025 + Serial Key
EaseUS Partition Master Crack 2025 + Serial Key
kherorpacca127
?
Endpoint Backup: 3 Reasons MSPs Ignore It
Endpoint Backup: 3 Reasons MSPs Ignore ItEndpoint Backup: 3 Reasons MSPs Ignore It
Endpoint Backup: 3 Reasons MSPs Ignore It
MSP360
?
Build with AI on Google Cloud Session #4
Build with AI on Google Cloud Session #4Build with AI on Google Cloud Session #4
Build with AI on Google Cloud Session #4
Margaret Maynard-Reid
?
Q4 2024 Earnings and Investor Presentation
Q4 2024 Earnings and Investor PresentationQ4 2024 Earnings and Investor Presentation
Q4 2024 Earnings and Investor Presentation
Dropbox
?
MIND Revenue Release Quarter 4 2024 - Finacial Presentation
MIND Revenue Release Quarter 4 2024 - Finacial PresentationMIND Revenue Release Quarter 4 2024 - Finacial Presentation
MIND Revenue Release Quarter 4 2024 - Finacial Presentation
MIND CTI
?

Unit testing by Svetlin Nakov

  • 1. UnitTesting Building Rock-Solid Software Svetlin Nakov Telerik Software Academy http://academy.telerik.com ManagerTechnicalTraining http://www.nakov.com
  • 3. UnitTest ¨C 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]
  • 4. UnitTest ¨C 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"); }
  • 5. UnitTesting ¨C 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 StudioTeamTest (VSTT) ? NUnit, MbUnit, Gallio, etc. 5
  • 7. UnitTesting 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 ¨C for C# and all .NET languages ? cppUnit, jsUnit, PhpUnit, PerlUnit, ... ? Visual StudioTeamTest (VSTT) ? Developed by Microsoft, integrated inVS 7
  • 8. UnitTesting Frameworks ? JUnit ¨C 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 ¨C 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
  • 9. JUnit ¨C 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>
  • 10. JUnit ¨C 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
  • 11. JUnit ¨C 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)
  • 12. JUnit ¨C Assertions (2) ? Conditions ? assertTrue(condition) ? assertFalse(condition) ? Forced test fail ? fail() ? Expecting exception ? @Test(expected=IndexOutOfBoundsExcept ion.class)
  • 13. ? Java class that needs unit testing: JUnit ¨C 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; } }
  • 14. ? JUnit based test class (fixture): JUnit ¨C 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); } }
  • 15. ? JUnit text fixture with setup and cleanup: JUnit ¨C 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; } }
  • 16. 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 { }
  • 17. Eclipse and JUnit ? Eclipse has built-in support for integration with JUnit ? Can create, run and debug JUnit tests
  • 19. §æ§à§â§å§Þ§á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö,§æ§à§â§å§Þ §å§Ö§Ò §Õ§Ú§Ù§Ñ§Û§ß §Ü§å§â§ã§à§Ó§Ö§Ú §å§â§à§è§Ú §á§à §á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö,§å§Ö§Ò §Õ§Ú§Ù§Ñ§Û§ß ¨C §Ò§Ö§Ù§á§Ý§Ñ§ä§ß§à §á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö§Ù§Ñ §Õ§Ö§è§Ñ ¨C §Ò§Ö§Ù§á§Ý§Ñ§ä§ß§Ú §Ü§å§â§ã§à§Ó§Ö §Ú §å§â§à§è§Ú §Ò§Ö§Ù§á§Ý§Ñ§ä§Ö§ßSEO §Ü§å§â§ã -§à§á§ä§Ú§Þ§Ú§Ù§Ñ§è§Ú§ñ §Ù§Ñ §ä§ì§â§ã§Ñ§é§Ü§Ú §å§â§à§è§Ú§á§à §å§Ö§Ò §Õ§Ú§Ù§Ñ§Û§ß, HTML,CSS, JavaScript,Photoshop §å§â§à§è§Ú§á§à §á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö§Ú §å§Ö§Ò §Õ§Ú§Ù§Ñ§Û§ß §Ù§Ñ §å§é§Ö§ß§Ú§è§Ú ASP.NETMVC§Ü§å§â§ã ¨C HTML,SQL,C#,.NET,ASP.NETMVC §Ò§Ö§Ù§á§Ý§Ñ§ä§Ö§ß§Ü§å§â§ã"§²§Ñ§Ù§â§Ñ§Ò§à§ä§Ü§Ñ §ß§Ñ §ã§à§æ§ä§å§Ö§â §Ó cloud §ã§â§Ö§Õ§Ñ" BGCoder -§à§ß§Ý§Ñ§Û§ß §ã§ì§ã§ä§Ö§Ù§Ñ§ä§Ö§Ý§ß§Ñ §ã§Ú§ã§ä§Ö§Þ§Ñ -online judge §Ü§å§â§ã§à§Ó§Ö§Ú §å§â§à§è§Ú §á§à §á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö,§Ü§ß§Ú§Ô§Ú ¨C §Ò§Ö§Ù§á§Ý§Ñ§ä§ß§à §à§ä§¯§Ñ§Ü§à§Ó §Ò§Ö§Ù§á§Ý§Ñ§ä§Ö§ß§Ü§å§â§ã"§¬§Ñ§é§Ö§ã§ä§Ó§Ö§ß §á§â§à§Ô§â§Ñ§Þ§Ö§ß§Ü§à§Õ" §Ñ§Ý§Ô§à§Ñ§Ü§Ñ§Õ§Ö§Þ§Ú§ñ ¨C §ã§ì§ã§ä§Ö§Ù§Ñ§ä§Ö§Ý§ß§à §á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö,§ã§ì§ã§ä§Ö§Ù§Ñ§ß§Ú§ñ ASP.NET§Ü§å§â§ã -§å§Ö§Ò §á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö,§Ò§Ñ§Ù§Ú §Õ§Ñ§ß§ß§Ú, C#,.NET,ASP.NET §Ü§å§â§ã§à§Ó§Ö§Ú §å§â§à§è§Ú §á§à §á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö¨C §´§Ö§Ý§Ö§â§Ú§Ü §Ñ§Ü§Ñ§Õ§Ö§Þ§Ú§ñ §Ü§å§â§ã§Þ§à§Ò§Ú§Ý§ß§Ú §á§â§Ú§Ý§à§Ø§Ö§ß§Ú§ñ §ã iPhone, Android,WP7,PhoneGap freeC#book, §Ò§Ö§Ù§á§Ý§Ñ§ä§ß§Ñ §Ü§ß§Ú§Ô§Ñ C#,§Ü§ß§Ú§Ô§Ñ Java,§Ü§ß§Ú§Ô§Ñ C# §¥§à§ß§é§à §®§Ú§ß§Ü§à§Ó -§ã§Ñ§Û§ä §Ù§Ñ §á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö §¯§Ú§Ü§à§Ý§Ñ§Û §¬§à§ã§ä§à§Ó -§Ò§Ý§à§Ô §Ù§Ñ §á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö C#§Ü§å§â§ã,§á§â§à§Ô§â§Ñ§Þ§Ú§â§Ñ§ß§Ö,§Ò§Ö§Ù§á§Ý§Ñ§ä§ß§à UnitTesting http://academy.telerik.com
  • 20. FreeTrainings @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