Top Banner
Copyright © 2012 Russel Winder 1 Testing: Python, Java, Groovy, etc. Prof Russel Winder http://www.russel.org.uk email: [email protected] xmpp: [email protected] twitter: @russel_winder
41

Testing: Python, Java, Groovy, etc.

May 11, 2015

Download

Technology

Russel Winder

Guest lecture on Laurie Tratt's course on testing at KCL, 2012-12-06.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 1

Testing: Python, Java, Groovy, etc.

Prof Russel Winder

http://www.russel.org.uk

email: [email protected]: [email protected]

twitter: @russel_winder

Page 2: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 2

Aims, Goals and Objects

● Look at some practical aspects of testing with Python, Java, and Groovy.

● Consider some of the practical positives and negatives of using test-driven development.

● Possibly† look at coverage as a useful tool for programmers‡.

†If time permits.‡And a dangerous weapon inthe hands of ignorant managers.

Page 3: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 3

Structure

Introduction.

First part.

Short break.

Second part.

Conclusion.

Page 4: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 4

Protocol

● Questions or short comments during the sessions are entirely in order.

● Let me know you have an interjection by raising your hand, and when I come to an appropriate pause, I'll pass you the token.

Questions, answers, comments, etc. appearingto get too long as interjections may get stackedto be unstacked at a break.

Page 5: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 5

Introduction

Page 6: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 6

Russel Winder

● Theoretical Physicist: quarks, strangeness, and charm.● UNIX systems programmer.● Academic at UCL: parallel programming, HCI, psychology

of programming.● Professor of Computing Science at KCL: health

informatics, parallel programming. Head of Department.● Starter of start-ups.● Independent consultant, analyst, author, expert witness,

trainer.

Page 7: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 7

Interstitial Advertisement

Page 8: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 8

Projects

● Gant● GPars● SCons● GroovyFX● Groovy● Gradle

● GroovyBalls● GFontBrowser● Pi_Quadrature● Sleeping_Barber

OK, so it's all open source.

Page 9: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 9

Testing RW Historically

● The FORTRAN years.● The C years.● Early Smalltalk and C++ years.● The Fortran years.● Early Java years.● Test-driven Development (TDD) hegemony.● Feature-Driven Development (FDD).● Behaviour-driven Development (BDD).

eXtreme Programming

Page 10: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 10

A Personal Epiphany

● C codes needs testing.● C is very difficult re testing.

● Use Python – as long as the C is in a dynamically linked library (aka shared object).

Page 11: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 11

The Two Parts

Page 12: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 12

The Terms

● Unit test.● Integration test.● System test.● Acceptance test.

Smoke test.

Mutation test.

Page 13: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 13

Unit Test

● Test the functions and methods for correct behaviour.

May well require use of mocks.

Page 14: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 14

Integration Test

● Test that combination of bits of the system collaborate in correct ways.

Probably needs some use of mocks.

Page 15: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 15

System Test

● Test that the system as a whole works as expected.

Smoke test – a pre-test to ensure it isworth running all the tests.

No mocks at all.

Page 16: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 16

Acceptance Test

● Test that the “purchaser” makes to be happy with the system as delivered.

Used to be crucial, now contracts are usuallynot for one-off items, but for annual support.

Page 17: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 17

Code under Test

Page 18: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 18

The Tools

● Test frameworks:● Unit testing● Integration testing● System testing.

Acceptance testing usually usesa different sort of tool, FitNessefor example.

http://fitnesse.org/

Programming languagespecific.

Page 19: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 19

Testing: A Backdrop

● Testing as we know it today has its roots in eXtreme Programming.● Smalltalk sUnit→● Python PyUnit (aka unittest)→● C++ cppUnit,…→● Java JUnit→

Every language created one or manyxUnit variants on the assumption it wasThe right thing to do…

…even when it wasn't.

Page 20: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 20

The xUnit Legacy

● xUnit is fundamentally grounded in dynamic languages – reflection required.

● Static languages like C++ need a very different approach, hence CUTE, Catch,… using template meta-programming (aka compile time reflection).

● Static languages like Java, can use annotations – still reflection based but better.

Page 21: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 21

In the Python-sphere

● PyUnit, aka unittest● py.test, aka PyTest● Nose, aka nose

There are many others but the aboveare the ones that matter.

Page 22: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 22

In the JVM-sphere

● Java:● JUnit3● JUnit4● TestNG

● Scala● ScalaTest● ScalaCheck● Specs2

● Groovy● GroovyTestCase

– JUnit3 in disguise

● Spock

● Clojure● clojure.test● Midje● lazytest

Kotlin, Ceylon, Jython, JRuby,…

Page 23: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 23

Being In Control

● For all software developments always use a version control system.

● Given DVCSs† such as Git, Mercurial, Bazaar, there are no excuses for not managing development with a version control system.

†Distributed version control system.

Page 24: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 24

Individuals / Teams

● Single developer projects need no other tools.● Multi-developer projects need more infrastructure.

Page 25: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 25

Continuous Integration

● A process via which all tests are run for all commits to the mainline repository.

● A continuous integration server monitors the mainline repository.

Page 26: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 26

Developer

Developer

DeveloperDeveloper

Developer

Mainline Repository

Continuous Integration

Deployment Server

Team Workflow

Continuous deliveryContinuous deployment

System Test Server

Page 27: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 27

Buildbot

● FOSS continuous integration framework written in Python:● Single master.● Multiple slaves.

http://buildbot.scons.org/

http://www.scons.org/

http://trac.buildbot.net/

Page 28: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 28

Atlassian Bamboo

● Commercial continuous integration server, free to FOSS organizations, e.g. Codehaus

http://www.atlassian.com/

http://www.codehaus.org/

http://gant.codehaus.org/

Page 29: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 29

JetBrains TeamCity

● Commercial continuous integration server.● Free support for FOSS projects.● Cloudy.

http://gpars.codehaus.org/

http://www.jetbrains.com/

Page 30: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 30

The Process

Red

Refactor

Green

New Test

Fix CUT

Page 31: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 31

Test-driven Development

● Never amend your code unless you have a failing test.

● Unless the change is a refactoring.

Run all your tests often.

Page 32: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 32

Refactoring

● Ensure there are no pending commits prior to a refactoring.

● Run all the tests immediately before a refactoring – ensure you get a green.

● Run all the tests immediately after a refactoring – ensure you get a green.

● If a refactoring leads to a red, back out of all the changes.

● Run the tests and ensure you get a green.

Page 33: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 33

Unit Testing and Mocks

● System testing done within a constrained real system not live.

● Unit and integration testing needs to disconnect from some resources.

Page 34: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 34

A shortMonty Python

moment…

Page 35: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 35

Page 36: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 36

A Short Coding Dojo

Page 37: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 37

Conclusion

Page 38: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 38

Testing

● It's what programmers do.

Page 39: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 39

Version Control

● It's the way programmers work.

Page 40: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 40

Interstitial Advertisement

Page 41: Testing: Python, Java, Groovy, etc.

Copyright © 2012 Russel Winder 41

Testing: Python, Java, Groovy, etc.

Prof Russel Winder

http://www.russel.org.uk

email: [email protected]: [email protected]

twitter: @russel_winder