testing with junit - uvic.cashsaad/seng426/resources/lab... · junit basics to define test cases:...
TRANSCRIPT
![Page 1: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/1.jpg)
TESTING WITH JUNIT
Lab 5 : Testing
![Page 2: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/2.jpg)
Overview
Testing with JUnit
JUnit Basics
Sample Test Case
How To Write a Test Case
Aggregating Test Cases
Running Tests with JUnit
JUnit plug-in for NetBeans
Running Tests in NetBeans
Next Week
![Page 3: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/3.jpg)
New Resources
Check the download page at the lab website to :
get the source code for the JUnit tutorial.
The tutorial use the JLinkedList.
![Page 4: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/4.jpg)
Testing with JUnit
JUnit is a simple testing framework for Java
It can be used to define “test cases”, which can be
grouped into “test suites”
These tests can be run to get a pass/fail indication
and a list of all failures
Can be downloaded from:
http://www.junit.org
![Page 5: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/5.jpg)
JUnit Basics
To define test cases:
Create a new class xxxTest that
extends TestCase
and import junit.framework.*
Define one or more testXXX() methods
Optionally define setUp() and tearDown() methods that are run before and after each test respectively
Can be used to initialize fields with test data common to all tests
Add static suite() and main methods
![Page 6: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/6.jpg)
Sample Test Case
package ca.uvic.seng426;import junit.framework.*;
public class VideoStoreTest extends TestCase {
public VideoStoreTest(String name) {super(name);
}
public static TestSuite suite() {return new TestSuite(VideoStoreTest.class);
}
public static void main (String[ ] args) {junit.textui.TestRunner.run (suite());
}
![Page 7: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/7.jpg)
Sample Test Case
private VideoStore store;
public void setUp() {
store = new VideoStore();
// … set up test data in the VideoStore …
}
public void tearDown() {
store = null;
}
public void testFindCustomer() {
Customer c = store.findCustomer(“12345”);
assertEquals(c.getName(), “John Locke”);
}
public void testAddMember() {
store.addMember(“98765”, “Michael Scofield”);
assertTrue(store.hasMember(“98765”));
}
}
![Page 8: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/8.jpg)
How to Write a Test Case
Signature
Always start with test
Follow with name of method or functionality tested
No arguments or return value
Body
Only test one point per method; keep it short
At the end, use one of the assert methods to check results:
assertEquals(exp, act) // checks equality
assertSame(exp, act) // checks identity
assertTrue(expr) // checks if condition is true
assertNull(obj) // checks if object is null
assertNotNull(obj) // checks if object is not null
fail() // fails (allows arbitrary check)
All these methods optionally take a failure message as the first argument.
![Page 9: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/9.jpg)
Aggregating Test Cases
Group test cases into test suites, nest suites recursively
public class VideoStoreTestSuite extends TestCase {
public VideoStoreTestSuite (String name) {
super(name);
}
public static TestSuite suite() {
TestSuite suite = new TestSuite();
suite.addTest(VideoStoreTest.suite());
suite.addTest(RentalTest.suite());
… etc.
return suite;
}
public static void main (String[ ] args) {
junit.textui.TestRunner.run (suite());
}
![Page 10: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/10.jpg)
Running Tests in NetBeans
We will Use Linked List Example
Download from The lab website JLinkedList Project.
Open JlinkedList with NetBeans.
![Page 11: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/11.jpg)
Testing JLinkedList with JUnit
The JlinkedList consist of two classes:
Node.java
List.java
![Page 12: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/12.jpg)
Node.java
![Page 13: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/13.jpg)
List.java
![Page 14: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/14.jpg)
Open JLinkedList Using NetBeans
Source Code Package
Test Package
Libraries (ex: JDK, etc)
Test Libraries (ex: JUnit3, JUnit4.5,etc)
![Page 15: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/15.jpg)
Create JUnit Test Classes
1- right click on source package
2- Go to Tools
3- Select Create
JUnit Tests
![Page 16: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/16.jpg)
Choose Tests Properties
Keep Default Selections
and click OK button
![Page 17: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/17.jpg)
Generated Test Classes
You will find the test classes inside
the Test Packages
![Page 18: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/18.jpg)
Exploring the Test Classes
ListTest.java is
the test class
for List.java
![Page 19: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/19.jpg)
Exploring the Test Classes
NodeTest.java
is the test class
for Node.java
![Page 20: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/20.jpg)
Exploring Test Function
Check testIsEmpty(), and testInsertNode() in the TestList.java Class
![Page 21: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/21.jpg)
Create Test Cases
What are the possible test cases for isEmpty() function?
The list is empty then the function should return true.
The list is not empty then the function should return false.
What are the possible test cases for insertNode()
function?
The list is Empty and the node is the first node in the list.
The list has one or more nodes and the new node will be
added to the end of the list.
The new node is already exist in the list, and so the insert
operation will be ignored.
![Page 22: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/22.jpg)
Test Cases for isEmpty()
![Page 23: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/23.jpg)
Test Cases for InsertNode()
![Page 24: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/24.jpg)
Test Cases for InsertNode()
Watch your testing
code you may
inject more bugs
You need to update the
listSize after each insert or
your end up adding bug
into your test code
![Page 25: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/25.jpg)
Execute Test Cases
You can execute your test cases by:
1. Right click on test Suite class and select
Run File.
2. Right click on the test class and select Run
File.
3. Right click on the Project Name and select
Test.
4. Press Alt+F6 or Shift+F6
![Page 26: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/26.jpg)
Test Result
![Page 27: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/27.jpg)
Next Week
ON June 15, 2011
TestLink Tutorials
How to use TestLink to document your test plan and test
cases.
Lab Assignment 2: Design, Write and Execute Test
Cases with junit.
Project Part 3
![Page 28: TESTING WITH JUNIT - UVic.cashsaad/seng426/resources/Lab... · JUnit Basics To define test cases: Create a new class xxxTest that extends TestCase and import junit.framework.* Define](https://reader030.vdocument.in/reader030/viewer/2022040611/5ed402a48d46b66d22634504/html5/thumbnails/28.jpg)
Questions?