Software Development Tools COMP220 Seb Coope Week 8 Lecture 1 Ant, Testing and JUnit (2) These slides are mainly based on “Java Development with Ant” -

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? © 2007 Christian Hujer.
Software Development Tools COMP220/COMP285 Seb Coope Ant and XML: Getting Started These slides are mainly based on “Java Development with Ant” - E. Hatcher.
GoogleTest Primer. Outline Basic Concepts Assertions Basic Assertions Binary Comparison String Comparison Floating-Point Comparison Simple Tests Test.
T ESTING WITH J UNIT IN E CLIPSE Farzana Rahman. I NTRODUCTION The class that you will want to test is created first so that Eclipse will be able to find.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
Ch. 2 Exploring core JUnit. This chapter covers ■ Using the core JUnit classes ■ Understanding JUnit mechanisms ■ Understanding the JUnit lifecycle.
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
ANT: Another Nice Tool Ali Beyad October 1, 2003.
ANT – Another Neat Tool Representation and Management of Data on the Internet.
ANT: Another Nice Tool Ali Beyad October 1, 2003.
Chapter 15: Discovering problems ● There are some tools that we can use to discover problems – Junit: for unit testing – Ant as a make utility – CVS as.
Approach of Unit testing with the help of JUnit Satish Mishra
Fall 2007CS 2251 Programming Tools Eclipse JUnit Testing make and ant.
1 Ant – Another Neat Tool Representation and Management of Data on the Internet.
Software Development Tools COMP220 Seb Coope Ant, Testing and JUnit (3) Capturing test results These slides are mainly based on “Java Development with.
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
14-Jul-15 JUnit 4. Comparing JUnit 3 to JUnit 4 All the old assertXXX methods are the same Most things are about equally easy JUnit 4 makes it easier.
Using Ant to build J2EE Applications Kumar
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
© Dr. A. Williams, Fall Present Software Quality Assurance – JUnit Lab 1 JUnit A unit test framework for Java –Authors: Erich Gamma, Kent Beck Objective:
Software Development Tools
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools.
How to Run a Java Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Software Development Tools COMP220/COMP285 Sebastian Coope Eclipse and JUnit: Creating and running a JUnit test case These slides are mainly based on “Java.
July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission.
(1) Unit Testing and Test Planning CS2110: SW Development Methods These slides design for use in lab. They supplement more complete slides used in lecture.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
Software Development COMP220/COMP285 Seb Coope Introducing Ant These slides are mainly based on “Java Development with Ant” - E. Hatcher & S.Loughran.
Week 14 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
JUnit A framework which provides hooks for easy testing of your Java code, as it's built Note: The examples from these slides can be found in ~kschmidt/public_html/CS265/Labs/Java/Junit.
Chapter 1 Introducing Ant. What is ant? Ant is a build tool  Automate the tasks of compiling code, running test, and packaging the results for redistribution.
JUnit. Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be.
CPSC 871 John D. McGregor Module 8 Session 2 JUnit.
Classes, Interfaces and Packages
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
PHPUnit Julia Bondar IAPM23. Agenda What is PHPUnit How to write an automated test Writing and running tests with PHPUnit Advantages and disadvantages.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Software Development Tools COMP220 Seb Coope Ant: Datatypes and Properties These slides are mainly based on “Java Development with Ant” - E. Hatcher &
Software Development Tools COMP220 Seb Coope More Ant Features These slides are mainly based on “Java Development with Ant” - E. Hatcher & S.Loughran.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Software Development COMP220 Seb Coope Ant: Structured Build These slides are mainly based on “Java Development with Ant” - E. Hatcher & S.Loughran. Manning.
Software Development Tools COMP220 Seb Coope Eclipse and Ant These slides are mainly based on “Java Development with Eclipse” – D.Gallardo et al., Manning.
Getting Started with JUnit Getting Started with JUnit The benefits and ease of writing and running JUnit test cases and test suites. The benefits and ease.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Software Development Tools
Software Development Tools
Unit Testing.
Software Development Tools
Software Construction Lab 10 Unit Testing with JUnit
Don Braffitt Updated: 26-Mar-2013
JUnit Automated Software Testing Framework
Introduction to JUnit CS 4501 / 6501 Software Testing
JUnit Automated Software Testing Framework
How to Run a Java Program
Introduction to JUnit IT323 – Software Engineering II
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CS 240 – Advanced Programming Concepts
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Review of Previous Lesson
Software Development Tools
Presentation transcript:

Software Development Tools COMP220 Seb Coope Week 8 Lecture 1 Ant, Testing and JUnit (2) These slides are mainly based on “Java Development with Ant” - E. Hatcher & S.Loughran. Manning Publications, 2003 and “JUnit in Action”, 2 nd edition – P. Tahchiev, F. Leme, V. Massol, G. Gregory, Manning Publications, 2011

JUnit Primer (with ANT) continued

3 More on writing a test case Recall that a test case is a class which imports some JUnit packages import org.junit.Test; import static org.junit.Assert.*;... and implements one or more no-argument methods testXXX() (See 11. Ant, Testing and JUnit, JUnit Primer, Slides 5, 6.) 11. Ant, Testing and JUnit where each testXXX() consists of several assertion methods typically comparing expected and actual values.

4 More on writing a test case: setUp() and tearDown() Additionally, to prevent method from affecting the behaviour of another we can a. annotate and implement a method public void setUp() which -initializes objects under test -by storing a state (fixture) as member variables to the test case b.Optionally, annotate and implement a method public void tearDown() which -releases objects/resources under test. c.For this to work, we also need to import from Junit: import org.junit.Before; import org.junit.After; The names setUp and tearDown are traditional after JUnit3. JUnit4 make the behaviour of setUp and tearDown as required. (See also next slides.)

Running a test case with setUp() and tearDown() Test Runner calls the same setUp() and tearDown() methods before and after method testXXX() is invoked. Thus, method testXXX() runs  under one and the same fixture created by setUp() (despite this fixture could probably be changed by other test methods testYYY() ),  hence, with no assumptions about the order in which test methods are called. This prevents one test from affecting the behaviour of another. The following example illustrates this behaviour by using System.out.println("some message"). 5

6 Test Case setUpTearDownTest.java : package org.example.antbook.junit; //import JUnit4 classes: import static org.junit.Assert.*; import org.junit.Test; // import org.junit.Before; //import org.junit.* also would work import org.junit.After; // public class //Runs before method public void setUp(){ System.out.println("setUp sets up a fixture"); //Runs after method public void tearDown(){ System.out.println("tearDown releases fixture"); public void testA(){ System.out.println("testA runs"); assertTrue("MULTIPLICATION FAILED!!!", 4 == (2 * 2)); //Each method annotated runs public void testB(){ System.out.println("testB runs"); assertSame("ADDITION FAILED!!!", 4, 2 + 2); } public void SomeTestC(){ System.out.println("SomeTestC runs"); assertSame("ADDITION FAILED!!!", 5, 2 + 2); } Should it fail/run ??? Put this file under ch04\test according to the package declaration. Use your personalised package name! Here setUp() and tearDown() methods do nothing besides informing that what they intend to methods

Compile and Run: C:\Antbook\ch04>javac -d build\test test\org\example\antbook\junit\setUpTearDownTest.java C:\Antbook\ch04>java -cp build\test;C:\JAVA\junit4.8.2\junit jar org.junit.runner.JUnitCore org.example.antbook.junit.setUpTearDownTest JUnit version setUp sets up a fixture testA runs tearDown releases fixture.setUp sets up a fixture testB runs tearDown releases fixture Time: OK (2 tests) Do not forget to recompile each time!!! Note that someTestC was not run, unlike testA and testB ! WHY?? Because annotated methods should be run by Junit4 test runners. testA() and testB(), succeeded, so, setUpTearDownTest succeeded too (OK). Replace 4 by 5 in testA() and recompile and run it again. What will be changed? Will setUpTearDownTest succeed? setUp and tearDown surround annotated method. 7

8 Compile and Run: C:\Antbook\ch04>java -cp build\test;C:\JAVA\junit4.8.2\junit jar org.junit.runner.JUnitCore org.example.antbook.junit.setUpTearDownTest JUnit version setUp sets up a fixture testA runs tearDown releases fixture E.setUp sets up a fixture testB runs tearDown releases fixture Time: There was 1 failure: 1) testA(org.example.antbook.junit.setUpTearDownTest) java.lang.AssertionError: MULTIPLICATION FAILED!!! FAILURES!!! Tests run: 2, Failures: 1 Do not forget to recompile each time!!! E means that the previous testA failed Your message to yourself annotation before someTestC, and compile and run it again. What will be the result? Finally, compile and run this by Ant from mybuild.xml like in Part 11, Slide 14.

Writing a Test Suite What if we need to run many JUnit test cases? JUnit, test classes can be grouped into a suite Just use appropriate JUnit import s and Grouping tests lets you build several individual test cases for a particular subsystem, and write an all-inclusive Test Suite that runs them all. See AllTests.java below. Note that Suite of Suites can also be created in the same way. 9

10 An example of a Test Suite package org.example.antbook; import org.junit.runner.RunWith; import org.junit.runners.Suite; {org.example.antbook.junit.SimpleTest.class, org.example.antbook.junit.setUpTearDownTest.class, org.eclipseguide.persistence.FilePersistenceServicesTest.class } ) public class AllTests{} C:\Antbook\ch04\test\org\example\antbook\AllTests.java Test Suite to run test methods of three test cases: SimpleTest, setUpTearDownTest and FilePersistenceServicesTest. We can also compose suites of suites in the same way. EXTEND target junit-TestRunner in mybuild.xml to RUN test suite AllTests.java. End of file

11 Test Suites and Ant?? Don’t need to bother with test suites when running JUnit tests using Ant:  Ant itself can list a group of TestCase classes in a much more powerful way – all test cases from a directory(!), and  run it as a batch from the build file itself.  But task is not appropriate for this purpose.

The JUnit task

13 The JUnit task The Ant's task is much better for running JUnit test cases than which we used till this point: runs one or more (even all from a directory) JUnit test cases Collects and displays test results in one or more formats Provides a way either to fail or continue a build when a test fails Recall the unnatural situation when BUILD SCCESSFUL while some tests failed. No need to mention explicitly any Test Runner (as it was necessary with task)

14 The JUnit task (cont.) Extend mybuild.xml by this target. You can also add: with id test.classpath formerly used for running test cases by and some tasks In order to execute some existing test cases by Ant, let us declare a special task with  the names of the test cases and  appropriate classpath : One or more test cases to run

15 The JUnit task (cont.) TRY it: H:\Antbook\ch04>ant -f mybuild.xml test-brief Buildfile: C:\Antbook\ch04\mybuild.xml [SOME MESSAGES OMITTED] test-brief: [junit] Test org.eclipseguide.persistence. FilePersistenceServicesTest FAILED BUILD SUCCESSFUL TEST FAILED, but BUILD SUCCESSFUL (?!) It would be more natural to see here also BUILD FAILED... But let us wait for a more advanced build with. Nothing above about org.example.antbook.junit.SimpleTest because it does not fail. TRY to confirm this by appropriate experiment.

16 The JUnit task (cont.) Test failure enforcing build failure There are two issues to note about these results: 1.no details were provided about which testXXX() method in FilePersistenceServicesTest failed and why,  not so bad – no cluttering messages,  But, on the other hand, insufficiently informative 2.the build completed successfully (?!) despite the test failure – looks not natural. However, ADD to the task two attributes: <junit haltonfailure="false" printsummary="true"> We now get more informative and coherent output: (a default value)

17 The JUnit task (cont.) Test failures enforcing build failures test-brief: [junit] Running org.eclipseguide.persistence.FilePersistenceServicesTest [junit] Tests run: 5, Failures: 2, Errors: 0, Time elapsed: sec [junit] Test org.eclipseguide.persistence.FilePersistenceServicesTest FAILED [junit] Running org.example.antbook.junit.SimpleTest [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: sec BUILD SUCCESSFUL Total time: 1 second printsummary="true" prints one-line statistics for each test case. Taking haltonfailure="true" gives results only up to the first failure: test-brief: [junit] Running org.eclipseguide.persistence.FilePersistenceServicesTest [junit] Tests run: 5, Failures: 2, Errors: 0, Time elapsed: sec BUILD FAILED C:\Antbook\ch04\mybuild.xml:140: Test org.eclipseguide.persistence.FilePersistenceServicesTest failed Now BUILD FAILED because of first failing test case(!!) Now it is more informative:

18 Structure directories to accommodate testing See also Part 11, slide 13 Recall and extend the directory structure in ch04 : C:\Antbook\ch04 - base directory ( basedir= "." ) C:\Antbook\ch04\src - source directory ( ${src.dir} ) C:\Antbook\ch04\test - test directory ( ${src.test.dir} ) containing (deeper) JUnit test classes C:\Antbook\ch04\build - build directory ( ${build.dir} ) C:\Antbook\ch04\build\classes - for compiled source files ( ${build.classes.dir} ) C:\Antbook\ch04\build\test - for compiled JUnit test cases ( ${build.test.dir} ) C:\Antbook\ch04\build\data - for test repots in XML format C:\Antbook\ch04\build\report - for test repots in HTML format (new directories data and report to be considered later) First 3 red coloured directories and their contents are created by yourself. Other directories should be created automatically by your Ant build file.

Four main steps to do in Ant build file related to JUnit testing Adding testing into our build process is straightforward: Add a few targets or tasks: 1. target test-init to initialize ( ) the testing directory structure (except directories created by yourself manually; these are last three directories on the previous slide), 2. target test-compile to compile the test code with, 3. target test (or the like) to execute the tests with (or ), 4. and tasks to generate the test reports (to be considered later). Note that besides testing we need as usually also steps and targets dealing with source code in src not mentioned above. 19

20 Ant build process related to JUnit testing Some more details More properties... Before above four steps, we should start with assigning properties to various directories used by our (compile and) test targets: C:\Antbook\ch04\build C:\Antbook\ch04\src C:\Antbook\ch04\test Benefits of presentation of directories by properties: If you want to change your system of directories, this can be done only in one place of build file.

Benefits of presentation of directories by properties: Also, we can easily control e.g. where Ant places test reports by overriding test.reports.dir in command line as -Dtest.reports.dir=C:\REPORTS to temporary change the standard (default) behaviour described in our Ant build file. Thus, in mybuild.xml you should present all directories by properties (except those directories related to package declarations which you create manually for your source files and test cases and need not mention in mybuild.xml )! Ant build process related to JUnit testing Some more details 21

22 For compiling and running source files and tests cases we need classpath which has been already introduced into mybuild.xml in 11. Ant, Testing and JUnit11. Ant, Testing and JUnit, Slides 14 and 29 (and repeated now): Ant build process related to JUnit testing Some more details CHECK all targets, tasks, properties and paths in mybuild.xml required for running test-brief target (and for other future test targets).

23 In mybuild.xml, testing can possibly be preceded by a task This brings over all non-.java resources: For example, assume that an html file is required as an argument of a Java program which should do something with this file (read it, parse it, search some information in it, etc). Then we need an example of such file test.html somewhere under C:\Antbook\ch04\test, and so test.html, should be copied into the ${build.test.dir}, to allow our compiled JUnit test cases to access test data file – test.html – easily. C:\Antbook\ch04\build\test C:\Antbook\ch04\test Ant build process related to JUnit testing Some more details