28
Testing with JUnit 1

Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

  • Upload
    others

  • View
    16

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

Testing with JUnit

1

Page 2: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

What are we doing here?

• Learning the mechanics of how to write tests in Java using JUnit • Without considering issues like coverage

• Using JUnit is sometimes called unit testing • Unit testing is great for testing the functionality of

classes • Unit testing is used in Test Driven Development

(TDD)

2

Page 3: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

Origins and current status

3

Page 4: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

Origins

• Kent Beck developed the first automated test tool for in mid-90’s • XUnit, developed for Smalltalk

• Beck and Gamma developed JUnit on a flight from Zurich to Washington DC • Martin Fowler: “Never in the field of software development was so much owed by so many to so few lines of code.”

4

4

Page 5: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

Currently

• Junit is now the standard tool for Test-Driven Development in Java

• Junit test generators now part of many Java IDEs (Eclipse, JBuilder, Intellij)

• Language similar to JUnit have been developed for other languages (Perl, C++, Python, Visual Basic, C#, …)

5

5

Page 6: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

A simple example

6

Page 7: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

Stack: A poor implementation

7

7

Page 8: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

But let’s test it anyway - detail follows

8

8

Page 9: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

One class for the tests

• Call it SomethingTest • Something = the class you are testing

• Put the test subject in the class as an instance variable

9

9

Page 10: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

A number of tests may appear in the class• Each called testSomething • Each prefixed with ‘@Test’ (no quotes, no ‘;’)

• This is a JUnit 4 feature, use JUnit 4

10

10

Page 11: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

Demo

• Explore (and run) the tests

11

11

Page 12: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

More detail

• Seems reasonable... !!!!

• But it assumes we have initialised ‘s’ , our stack implementation

12

12

Page 13: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

Pre test-initialisation

• Use the ‘@Before’ tag for the method ‘setUp’ • setUp is then run before each test !!!!

13

13

Page 14: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

What if we didn’t have a pre-test setup?• Eg....

14

14

Page 15: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

What’s the problem?• JUnit’s test runner can run all the tests in our test

class, assume we run these tests in this order..

15

15

Page 16: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

Summary: To provide test independence• We need to set up tests to run independently • ‘@Before’ tags a setup method to

run immediately before each test • Similarly ‘@After’ tags a teardown method to run

after each test runs • Do we need a teardown method for this example? !

• Default behaviour for independent tests • Tests run in a random order = good practice • You can change this, see the next slides and

the JUnit documentation16

16

Page 17: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

Test order

• Not determined, eg Java 1.7 executes tests in a random order

• Annotate the class if you want a particular order !!!!

• Ordering • JVM, DEFAULT, NAME_ASCENDING • Nothing for ‘in-class declaration’ order

17

17

Page 18: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

Test order example

18

18

Page 19: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

On screen demo

• Let’s play around with the tests.... • Wow, with a test suite we can refactor our

implementation safely • I did that to provide a better stack implementation

19

19

Page 20: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

More about assertions, good style

20

Page 21: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

assertThat and matcher statements

• Using ‘assertThat’ improves readability • assert that x is 3 • assertThat( x, is(3) ); • assertThat(<value>, is(<expected value>)) • assertThat(<value>, <matcher statement>)

• Matcher statements • is(....), lessThan(....), sameInstance(....) • Matchers are defined in

• org.hamcrest.CoreMatchers • Only two non-deprecated matchers in

org.junit.matchers.JUnitMatchers 21

21

Page 22: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

Old and new styles

!!!!!!!

• Note the extra (first) argument to ‘assertThat’ • do we need it?

• Demo both tests • Note different output 22

22

Page 23: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

Some matchers

• equalTo(....) • is(....) • not(....) note not(equalTo(....)) etc • nullValue(....), notNullValue(....) • instanceOf(....) • hasItemInArray(....)

23

23

Page 24: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

Combining matchers

• There are ways of writing assertions to perform logical operations

• Lets test that a variable ‘responseString’ contains either ‘colour’ or ‘color’assertThat( responseString, anyOf( containsString("color"), containsString("colour") ) );

!• This kind of construct gives great failure messagesExpected: (a string containing "color" or a string containing "colour") Got: "Utter, complete and total darkness""

24

24

Page 25: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

Find out more for yourselves

• Read this • https://github.com/junit-team/junit/wiki/Assertions

• Comprehensive example of assertions and matchers from JUnit and Hamcrest

• Follow these up if you want/need to • Timeouts for tests

https://github.com/junit-team/junit/wiki/Timeout-for-tests • Fixtures for tests in a class

(an ‘@Before’ for all tests in a class)https://github.com/junit-team/junit/wiki/Timeout-for-tests

• Running from the command line https://github.com/junit-team/junit/wiki/Test-runners

25

25

Page 26: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

Finally, this may help you

26

Page 27: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

Ignoring a test

• Put @Ignore on the line above @Test

27

27

Page 28: Testing with JUnit - University of Manchesterstudentnet.cs.manchester.ac.uk/pgt/2015/COMP61511/slides/week3/… · • Junit test generators now part of many Java IDEs (Eclipse, JBuilder,

Test cycle: Red, green, refactor

28

GREENRED

https://octoberclub.wordpress.com/2011/10/04/tidying-up-technical-debt-using-tdd/

28