KEMBAR78
Unit Tests? It is Very Simple and Easy! | PPT
© 2015 Return on Intelligence, Inc. Confidential 1
Traning for beginers in UT development
Designed by LibreOffice, Kubuntu 14.04, A.Tylevich, 2016
Unit Tests ? It is very simple and easy!
© 2015 Return on Intelligence, Inc. Confidential 2
About training
This is introduction.
Tries to explain why all this is necessary.
A little of code, more philosophy and history
Why ?
For whom ?
Designed by LibreOffice, Kubuntu 14.04
© 2015 Return on Intelligence, Inc. Confidential 3
Content
Historical reference
What are Unit Tests ?
UT - what for they are necessary (philosophy)
What is the Unit and what can be the Unit
Approaches in UT
Without frameworks (stub classes)
Frameworks (examples, annotations)
UT frameworks
Junit 3, 4 (Asserts …)
TestNG
Mock frameworks
JMock
EasyMock
Mockito
EJB Mock Container
Additional frameworks
Equals Verifier
Addition of dependences in pom.xml
Code Coverage
Cobertura
Covering examples
… tools
Automation tests
QTP, Selenium
FitNesse
Links to literature and sources
Designed by LibreOffice, Kubuntu 14.04
© 2015 Return on Intelligence, Inc. Confidential 4
One of the first
attempts to
create a
programming
language, from
1943 to 1945
1940 1980 1990
In the early of 1950s
the quantity of the
developed software
increased, and
programs became
very big
1950
FORTRAN(1954)
ALGOL(1960)
Mashine
languages
Pascal(1970),
C(1972),
SQL(1978)
C++(1980),
Ada(1983),
Perl(1987)
PHP(1995),
Java(1995),
JavaScript(1995)
Some important
languages were
developed:
Python (1991),
Ruby (1993),
Delphi (1995), ...
Improvement of
the paradigms
defined in the
previous
decades
NewParadigms
and…
Historical reference
Programming languages
Designed by LibreOffice, Kubuntu 14.04
1960
New languages
appear:
Smalltalk (1972)
1970
© 2015 Return on Intelligence, Inc. Confidential 5
Historical reference
Designed by LibreOffice, Kubuntu 14.04
Testing
1980
Correctness->
searchforissues
Preventionof
mistakes
Designand
supportoftests
Early 1990s, the testing
includes planning, design,
support and implementation
of tests.
The testing starts providing
quality of the software.
1980s, a concept of the
prevention of defects appears.
Tests - method of the
prevention of issues.
Appears methodology of testing
on all cycle of development.
Exhaustive
testing
Early 1970s, testing was
presented as "activities for
confirmation of correctness of
work of the software"
From confirmations to search for
issues
1970 19901960
© 2015 Return on Intelligence, Inc. Confidential 6
Framework SUnit appears for Smalltalk language in 1998 .
Designed by LibreOffice, Kubuntu 14.04
Historical reference
Testing
SUnit represented structure and functionality to xUnit.
CppUnit - a framework for C ++
DUnit - the tool for the environment of development of Delphi
JUnit - library for Java
NUnit - environment a unit testing for .NET
phpUnit - library for PHP
vbUnit - Visual Basic
JsUnit (Jasmine) - JavaScript
© 2015 Return on Intelligence, Inc. Confidential 7
What are Unit Tests
Unit Testing is a software testing process by which individual module of
source code ... are tested to determine whether they are fit for use.
What is the process?
What is module ?
Process — implementation, writing and run of Unit Tests
Module — atomic part of a code
Designed by LibreOffice, Kubuntu 14.04
Unit testing, wikipedia
© 2015 Return on Intelligence, Inc. Confidential 8
UT - what for they are necessary
In the 1990s testing passed to ensuring the quality covering all cycle of development
You constantly are engaged in testing
(This is integration testing: "Test" buttons or class with name TestServer)
Some customers demand a very high level of testing
You need to understand how code works
You need to write a part of code, but you have no opportunity to test it in actual
environment and you exactly know what data will come to methods
It isn't necessary to write unit tests, if
You do the simple site of 3-5 html-pages with one form of sending the e-mail
You are developed a simple flash games or banners
You do the project for an exhibition
You always write a code without mistakes, you have an ideal brain and gift of
anticipation. Your code is so cool that it changes itself according to customer
requirements. Periodically the code explains to the customer that his requirements
don't need to be realized
Designed by LibreOffice, Kubuntu 14.04
Philosophy
© 2015 Return on Intelligence, Inc. Confidential 9
What is the Unit
Program
Module
Class
Method
That you will think up
Unit -
small self-sufficient part of a code realizing some behavior.
Designed by LibreOffice, Kubuntu 14.04
What can be Unit
© 2015 Return on Intelligence, Inc. Confidential 10
Approaches in UT
UT
simple atomic testing
TDD
(Test-Driven Development)
Development through testing.
Designed by LibreOffice, Kubuntu 14.04
UT -> TDD -> BDD -> DDT
© 2015 Return on Intelligence, Inc. Confidential 11
Approaches in UT
BDD
(Behavior Driven Development)
BDD is further evolution of ideas of TDD.
DDT
(Data-Driven Testing)
Methodology of the testing operated by data.
Designed by LibreOffice, Kubuntu 14.04
UT -> TDD -> BDD -> DDT
© 2015 Return on Intelligence, Inc. Confidential 12
Without frameworks
RDP ?
RDP !
Designed by LibreOffice, Kubuntu 14.04
Or how it was earlier ?
Frameworks
Or how it is now ?
RDP.
RDP.
© 2015 Return on Intelligence, Inc. Confidential 13
Framework JUnit 3 JUnit 4
Java version Java 1.4 Java 1.5
Extends TestCase -
Overrides setUp & tearDown annotations
Annotations -
@Before
@After
@Test
@Ignore
Repeated RepeatedTest @Repeat( times = xxx )
UT Frameworks
JUnit 3, 4 (Asserts…)
Designed by LibreOffice, Kubuntu 14.04
© 2015 Return on Intelligence, Inc. Confidential 14
UT Frameworks
Designed by LibreOffice, Kubuntu 14.04
TestNG
Framework JUnit 4 TestNG
Test annotation @Test @Test
Start before the suit – @BeforeSuite
Start after the suit – @AfterSuite
Start before test – @BeforeTest
Start after test – @AfterTest
Start before test from group – @BeforeGroups
Start after test from group – @AfterGroups
Start before a class @BeforeClass @BeforeClass
Start after a class @AfterClass @AfterClass
Start before each test method @Before @BeforeMethod
Start after each test method @After @AfterMethod
To ignore the test @Ignore @Test(enable=false)
To expect an exception @Test (expected = Exception.class) @Test (expectedExceptions = Exception.class)
Timeout @Test (timeout = 1000) @Test (timeout = 1000)
© 2015 Return on Intelligence, Inc. Confidential 15
UT Frameworks
Designed by LibreOffice, Kubuntu 14.04
Addition of dependences into pom.xml
JUnit 3 JUnit 4 TestNG
<dependencies>
...
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencies>
...
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencies>
...
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<scope>test</scope>
</dependency>
</dependencies>
© 2015 Return on Intelligence, Inc. Confidential 16
Mock frameworks
Designed by LibreOffice, Kubuntu 14.04
And additional frameworks
JMock
Mockito
EasyMock
EJB Mock Container
Equals Verifier
Framework JMock Mockito
Type Proxy-based Proxy-based
Stubbing Yes Yes
Default return values 0, empty String, false, empty array, null 0, empty String, false, empty array, null
Instances are Strict Non-strict
Declarative No Yes
Interface additional efforts From box
Abstract class additional efforts ?
Class additional efforts From box
© 2015 Return on Intelligence, Inc. Confidential 17
Mock frameworks
Designed by LibreOffice, Kubuntu 14.04
Addition of dependences into pom.xml
JMock Mockito Equals Verifier
<dependencies>
...
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock</artifactId>
<version>2.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencies>
...
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencies>
…
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>1.7.7</version>
<scope>test</scope>
</dependency>
</dependencies>
© 2015 Return on Intelligence, Inc. Confidential 18
Code Coverage
Designed by LibreOffice, Kubuntu 14.04
Cobertura
if (x == 0) {
System.out.println("X is zero");
} else {
System.out.println("X is invalid");
}
© 2015 Return on Intelligence, Inc. Confidential 19
Code Coverage
Designed by LibreOffice, Kubuntu 14.04
Addition of Cobertura dependences into pom.xml
Dependency section Build section Reporting section
<dependencies>
...
<dependency>
<groupId>
net.sourceforge.cobertura
</groupId>
<artifactId>cobertura</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
...
<plugin>
<groupId>
org.codehaus.mojo
</groupId>
<artifactId>
cobertura-maven-plugin
</artifactId>
<version>2.6</version>
<configuration>
<check>
<...Rate> ...</...Rate>
</check>
<formats>
<format>html</format>
</formats>
<instrumentation>
<excludes>
<exclude> ... /*.class</exclude>
</excludes>
</instrumentation>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
...
<plugin>
<groupId>
org.codehaus.mojo
</groupId>
<artifactId>
cobertura-maven-
plugin
</artifactId>
<version>2.6</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
</plugin>
</plugins>
</reporting>
© 2015 Return on Intelligence, Inc. Confidential 20
Automation tests
Designed by LibreOffice, Kubuntu 14.04
QTP, Selenium
QTP Selenium
Commercial tool Open source tool
Tests can only be developed in QTP IDE Wide range of IDEs like Visual Studio,
Eclipse, Netbeans
Oly VB script Supports JAVA, .NET, Ruby, Perl, PHP, and
many other
User friendly and scripts are developed quickly Script requires more time to develop and
high skills
Latest version of HP ALM supports code that was
developed 5 years back
With new release of API the Test Scripts
need to be updated
© 2015 Return on Intelligence, Inc. Confidential 21
Automation tests
Designed by LibreOffice, Kubuntu 14.04
FitNesse
© 2015 Return on Intelligence, Inc. Confidential 22
Example
Designed by LibreOffice, Kubuntu 14.04
Why UT are necessary
© 2015 Return on Intelligence, Inc. Confidential 23
Questions
Designed by LibreOffice, Kubuntu 14.04
© 2015 Return on Intelligence, Inc. Confidential 24
Links
Designed by LibreOffice, Kubuntu 14.04
History of programming languages
https://en.wikipedia.org/wiki/History_of_programming_languages
TDD
https://en.wikipedia.org/wiki/Test-driven_development
BDD
https://en.wikipedia.org/wiki/Behavior-driven_development
xUnit
https://en.wikipedia.org/wiki/XUnit
JUnit
http://junit.org/cookbook.html
Open Source Testing Tools in Java
http://java-source.net/open-source/testing-tools
Cobertura
https://github.com/cobertura/cobertura/wiki/Roadmap
http://cobertura.github.io/cobertura/
eCobertura (Eclipce plugin)
http://ecobertura.johoop.de/
EqualsVerifier
http://www.jqno.nl/equalsverifier/
© 2015 Return on Intelligence, Inc. Confidential 25
Links to webinars
Designed by LibreOffice, Kubuntu 14.04
Вебинар "Introduction to Automation Testing"
http://www.returnonintelligence.ru/webinars/introduction-to-automation-testing
Вебинар "Code Coverage“
http://www.returnonintelligence.ru/webinars/code-coverage
Вебинар "Testing Your Code“ (TDD)
http://www.returnonintelligence.ru/webinars/testingyourcode
Вебинар "Эволюция к Behavior Driven Development на примере популярного фреймворка
JBehave"
http://www.returnonintelligence.ru/webinars/bdd

Unit Tests? It is Very Simple and Easy!

  • 1.
    © 2015 Returnon Intelligence, Inc. Confidential 1 Traning for beginers in UT development Designed by LibreOffice, Kubuntu 14.04, A.Tylevich, 2016 Unit Tests ? It is very simple and easy!
  • 2.
    © 2015 Returnon Intelligence, Inc. Confidential 2 About training This is introduction. Tries to explain why all this is necessary. A little of code, more philosophy and history Why ? For whom ? Designed by LibreOffice, Kubuntu 14.04
  • 3.
    © 2015 Returnon Intelligence, Inc. Confidential 3 Content Historical reference What are Unit Tests ? UT - what for they are necessary (philosophy) What is the Unit and what can be the Unit Approaches in UT Without frameworks (stub classes) Frameworks (examples, annotations) UT frameworks Junit 3, 4 (Asserts …) TestNG Mock frameworks JMock EasyMock Mockito EJB Mock Container Additional frameworks Equals Verifier Addition of dependences in pom.xml Code Coverage Cobertura Covering examples … tools Automation tests QTP, Selenium FitNesse Links to literature and sources Designed by LibreOffice, Kubuntu 14.04
  • 4.
    © 2015 Returnon Intelligence, Inc. Confidential 4 One of the first attempts to create a programming language, from 1943 to 1945 1940 1980 1990 In the early of 1950s the quantity of the developed software increased, and programs became very big 1950 FORTRAN(1954) ALGOL(1960) Mashine languages Pascal(1970), C(1972), SQL(1978) C++(1980), Ada(1983), Perl(1987) PHP(1995), Java(1995), JavaScript(1995) Some important languages were developed: Python (1991), Ruby (1993), Delphi (1995), ... Improvement of the paradigms defined in the previous decades NewParadigms and… Historical reference Programming languages Designed by LibreOffice, Kubuntu 14.04 1960 New languages appear: Smalltalk (1972) 1970
  • 5.
    © 2015 Returnon Intelligence, Inc. Confidential 5 Historical reference Designed by LibreOffice, Kubuntu 14.04 Testing 1980 Correctness-> searchforissues Preventionof mistakes Designand supportoftests Early 1990s, the testing includes planning, design, support and implementation of tests. The testing starts providing quality of the software. 1980s, a concept of the prevention of defects appears. Tests - method of the prevention of issues. Appears methodology of testing on all cycle of development. Exhaustive testing Early 1970s, testing was presented as "activities for confirmation of correctness of work of the software" From confirmations to search for issues 1970 19901960
  • 6.
    © 2015 Returnon Intelligence, Inc. Confidential 6 Framework SUnit appears for Smalltalk language in 1998 . Designed by LibreOffice, Kubuntu 14.04 Historical reference Testing SUnit represented structure and functionality to xUnit. CppUnit - a framework for C ++ DUnit - the tool for the environment of development of Delphi JUnit - library for Java NUnit - environment a unit testing for .NET phpUnit - library for PHP vbUnit - Visual Basic JsUnit (Jasmine) - JavaScript
  • 7.
    © 2015 Returnon Intelligence, Inc. Confidential 7 What are Unit Tests Unit Testing is a software testing process by which individual module of source code ... are tested to determine whether they are fit for use. What is the process? What is module ? Process — implementation, writing and run of Unit Tests Module — atomic part of a code Designed by LibreOffice, Kubuntu 14.04 Unit testing, wikipedia
  • 8.
    © 2015 Returnon Intelligence, Inc. Confidential 8 UT - what for they are necessary In the 1990s testing passed to ensuring the quality covering all cycle of development You constantly are engaged in testing (This is integration testing: "Test" buttons or class with name TestServer) Some customers demand a very high level of testing You need to understand how code works You need to write a part of code, but you have no opportunity to test it in actual environment and you exactly know what data will come to methods It isn't necessary to write unit tests, if You do the simple site of 3-5 html-pages with one form of sending the e-mail You are developed a simple flash games or banners You do the project for an exhibition You always write a code without mistakes, you have an ideal brain and gift of anticipation. Your code is so cool that it changes itself according to customer requirements. Periodically the code explains to the customer that his requirements don't need to be realized Designed by LibreOffice, Kubuntu 14.04 Philosophy
  • 9.
    © 2015 Returnon Intelligence, Inc. Confidential 9 What is the Unit Program Module Class Method That you will think up Unit - small self-sufficient part of a code realizing some behavior. Designed by LibreOffice, Kubuntu 14.04 What can be Unit
  • 10.
    © 2015 Returnon Intelligence, Inc. Confidential 10 Approaches in UT UT simple atomic testing TDD (Test-Driven Development) Development through testing. Designed by LibreOffice, Kubuntu 14.04 UT -> TDD -> BDD -> DDT
  • 11.
    © 2015 Returnon Intelligence, Inc. Confidential 11 Approaches in UT BDD (Behavior Driven Development) BDD is further evolution of ideas of TDD. DDT (Data-Driven Testing) Methodology of the testing operated by data. Designed by LibreOffice, Kubuntu 14.04 UT -> TDD -> BDD -> DDT
  • 12.
    © 2015 Returnon Intelligence, Inc. Confidential 12 Without frameworks RDP ? RDP ! Designed by LibreOffice, Kubuntu 14.04 Or how it was earlier ? Frameworks Or how it is now ? RDP. RDP.
  • 13.
    © 2015 Returnon Intelligence, Inc. Confidential 13 Framework JUnit 3 JUnit 4 Java version Java 1.4 Java 1.5 Extends TestCase - Overrides setUp & tearDown annotations Annotations - @Before @After @Test @Ignore Repeated RepeatedTest @Repeat( times = xxx ) UT Frameworks JUnit 3, 4 (Asserts…) Designed by LibreOffice, Kubuntu 14.04
  • 14.
    © 2015 Returnon Intelligence, Inc. Confidential 14 UT Frameworks Designed by LibreOffice, Kubuntu 14.04 TestNG Framework JUnit 4 TestNG Test annotation @Test @Test Start before the suit – @BeforeSuite Start after the suit – @AfterSuite Start before test – @BeforeTest Start after test – @AfterTest Start before test from group – @BeforeGroups Start after test from group – @AfterGroups Start before a class @BeforeClass @BeforeClass Start after a class @AfterClass @AfterClass Start before each test method @Before @BeforeMethod Start after each test method @After @AfterMethod To ignore the test @Ignore @Test(enable=false) To expect an exception @Test (expected = Exception.class) @Test (expectedExceptions = Exception.class) Timeout @Test (timeout = 1000) @Test (timeout = 1000)
  • 15.
    © 2015 Returnon Intelligence, Inc. Confidential 15 UT Frameworks Designed by LibreOffice, Kubuntu 14.04 Addition of dependences into pom.xml JUnit 3 JUnit 4 TestNG <dependencies> ... <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <dependencies> ... <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <dependencies> ... <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.10</version> <scope>test</scope> </dependency> </dependencies>
  • 16.
    © 2015 Returnon Intelligence, Inc. Confidential 16 Mock frameworks Designed by LibreOffice, Kubuntu 14.04 And additional frameworks JMock Mockito EasyMock EJB Mock Container Equals Verifier Framework JMock Mockito Type Proxy-based Proxy-based Stubbing Yes Yes Default return values 0, empty String, false, empty array, null 0, empty String, false, empty array, null Instances are Strict Non-strict Declarative No Yes Interface additional efforts From box Abstract class additional efforts ? Class additional efforts From box
  • 17.
    © 2015 Returnon Intelligence, Inc. Confidential 17 Mock frameworks Designed by LibreOffice, Kubuntu 14.04 Addition of dependences into pom.xml JMock Mockito Equals Verifier <dependencies> ... <dependency> <groupId>org.jmock</groupId> <artifactId>jmock</artifactId> <version>2.8.1</version> <scope>test</scope> </dependency> </dependencies> <dependencies> ... <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.10.19</version> <scope>test</scope> </dependency> </dependencies> <dependencies> … <dependency> <groupId>nl.jqno.equalsverifier</groupId> <artifactId>equalsverifier</artifactId> <version>1.7.7</version> <scope>test</scope> </dependency> </dependencies>
  • 18.
    © 2015 Returnon Intelligence, Inc. Confidential 18 Code Coverage Designed by LibreOffice, Kubuntu 14.04 Cobertura if (x == 0) { System.out.println("X is zero"); } else { System.out.println("X is invalid"); }
  • 19.
    © 2015 Returnon Intelligence, Inc. Confidential 19 Code Coverage Designed by LibreOffice, Kubuntu 14.04 Addition of Cobertura dependences into pom.xml Dependency section Build section Reporting section <dependencies> ... <dependency> <groupId> net.sourceforge.cobertura </groupId> <artifactId>cobertura</artifactId> <version>2.1.1</version> </dependency> </dependencies> <build> <plugins> ... <plugin> <groupId> org.codehaus.mojo </groupId> <artifactId> cobertura-maven-plugin </artifactId> <version>2.6</version> <configuration> <check> <...Rate> ...</...Rate> </check> <formats> <format>html</format> </formats> <instrumentation> <excludes> <exclude> ... /*.class</exclude> </excludes> </instrumentation> </configuration> </plugin> </plugins> </build> <reporting> <plugins> ... <plugin> <groupId> org.codehaus.mojo </groupId> <artifactId> cobertura-maven- plugin </artifactId> <version>2.6</version> <configuration> <formats> <format>html</format> <format>xml</format> </formats> </configuration> </plugin> </plugins> </reporting>
  • 20.
    © 2015 Returnon Intelligence, Inc. Confidential 20 Automation tests Designed by LibreOffice, Kubuntu 14.04 QTP, Selenium QTP Selenium Commercial tool Open source tool Tests can only be developed in QTP IDE Wide range of IDEs like Visual Studio, Eclipse, Netbeans Oly VB script Supports JAVA, .NET, Ruby, Perl, PHP, and many other User friendly and scripts are developed quickly Script requires more time to develop and high skills Latest version of HP ALM supports code that was developed 5 years back With new release of API the Test Scripts need to be updated
  • 21.
    © 2015 Returnon Intelligence, Inc. Confidential 21 Automation tests Designed by LibreOffice, Kubuntu 14.04 FitNesse
  • 22.
    © 2015 Returnon Intelligence, Inc. Confidential 22 Example Designed by LibreOffice, Kubuntu 14.04 Why UT are necessary
  • 23.
    © 2015 Returnon Intelligence, Inc. Confidential 23 Questions Designed by LibreOffice, Kubuntu 14.04
  • 24.
    © 2015 Returnon Intelligence, Inc. Confidential 24 Links Designed by LibreOffice, Kubuntu 14.04 History of programming languages https://en.wikipedia.org/wiki/History_of_programming_languages TDD https://en.wikipedia.org/wiki/Test-driven_development BDD https://en.wikipedia.org/wiki/Behavior-driven_development xUnit https://en.wikipedia.org/wiki/XUnit JUnit http://junit.org/cookbook.html Open Source Testing Tools in Java http://java-source.net/open-source/testing-tools Cobertura https://github.com/cobertura/cobertura/wiki/Roadmap http://cobertura.github.io/cobertura/ eCobertura (Eclipce plugin) http://ecobertura.johoop.de/ EqualsVerifier http://www.jqno.nl/equalsverifier/
  • 25.
    © 2015 Returnon Intelligence, Inc. Confidential 25 Links to webinars Designed by LibreOffice, Kubuntu 14.04 Вебинар "Introduction to Automation Testing" http://www.returnonintelligence.ru/webinars/introduction-to-automation-testing Вебинар "Code Coverage“ http://www.returnonintelligence.ru/webinars/code-coverage Вебинар "Testing Your Code“ (TDD) http://www.returnonintelligence.ru/webinars/testingyourcode Вебинар "Эволюция к Behavior Driven Development на примере популярного фреймворка JBehave" http://www.returnonintelligence.ru/webinars/bdd