Smalltalk Testing - SUnit Programming Languages HUJI 2010 Yardena Meymann
Testing Code needs to be tested Compilers are capable of checking some trivial errors, this is usually not enough Several types of tests exist: unit, functional, integration, performance, stress, smoke (a.k.a. sanity), … In a dynamically typed language, unit testing is integral part of the development
What are test for? Tests are an executable specification of the functionality that they cover, always synchronized with the code Tests increase the likelihood that the code is correct – finding bugs quickly TDD – writing tests first, improves interfaces - you see your code from the client’s point of view The presence of tests gives you the courage to make structural changes to the code: refactoring - essential to prevent creeping entropy
Test Driven Development When creating fresh code, introducing a feature or fixing a bug: first write tests see them fail write code that makes tests succeed see them pass re-run tests whenever code is changed (regression) keep tests passing, fixing code and refactoring tests if needed
Properties of Test repeatable run without human intervention act as a scenario stable - change frequency lower than the one of the covered functionality number of tests should be proportional to the number of tested functionalities
SUnit Introduction Minimal yet powerful framework “the mother of unit test frameworks” developed originally by Kent Beck SUnit allows to structure tests, describe the context of tests and to run them automatically
SUnit Simple test class extends TestCase setUp method – initialization (fixture) Methods that start with test Each method represent a single test Within methods use assert: condition or deny:condition Executing a test: MyTest run: #testMe Executing all tests: MyTest suite run Clean up with tearDown method
Example Test 'Sample-Test' SampleTest = TestCase ( | emptySet fullSet | 'as yet unclassified' setUp = ( emptySet := Set new. f ullSet := Set with: 5 with: #abc ) testIncludes = ( self assert: (fullSet includes: 5). self assert: (fullSet includes: #abc) testOccurrences = ( self assert: (emptySet occurrencesOf: 0) = 0. self assert: (fullSet occurrencesOf: 5) = 1. fullSet add: 5. self assert: (fullSet occurrencesOf: 5) = 1 testRemove = ( fullSet remove: 5. self assert: (fullSet includes: #abc). self deny: (fullSet includes: 5)
Hopscotch Test Runner
Hopscotch Test Runner