Presentation is loading. Please wait.

Presentation is loading. Please wait.

Testing and Build JUnit and Ant. JUnit Testing is not closely integrated with development. This prevents you from measuring the progress of development-

Similar presentations


Presentation on theme: "Testing and Build JUnit and Ant. JUnit Testing is not closely integrated with development. This prevents you from measuring the progress of development-"— Presentation transcript:

1 Testing and Build JUnit and Ant

2 JUnit Testing is not closely integrated with development. This prevents you from measuring the progress of development- you can't tell when something starts working or when something stops working. Using JUnit you can cheaply and incrementally build a test suite that will help you measure your progress, spot unintended side effects, and focus your development efforts.

3 Content The Problem Example Testing Practices

4 The problem Every programmer knows they should write tests for their code. Few do. This quickly becomes a vicious cycle- the more pressure you feel, the fewer tests you write. The fewer tests you write, the less productive you are and the less stable your code becomes. The less productive and accurate you are, the more pressure you feel. Breaking out requires an outside influence. The outside influence we needed in a simple testing framework that lets us do a little testing that makes a big difference.

5 Example Let's start simple and define a class Money to represent a value in a single currency. We represent the amount by a simple int. We represent a currency as a string holding the ISO three letter abbreviation (USD, CHF, etc.).

6 class Money { private int fAmount; private String fCurrency; public Money(int amount, String currency) { fAmount= amount; fCurrency= currency; } public int amount() { return fAmount; } public String currency() { return fCurrency; }

7 public Money add(Money m) { return new Money(amount()+m.amount(), currency()); } public boolean equals(Object anObject) { if (anObject instanceof Money) { Money aMoney= (Money) anObject; return aMoney.currency().equals(currency()) && amount() == aMoney.amount(); } return false; }

8 JUnit defines how to structure your test cases and provides the tools to run them. You implement a test in a subclass of TestCase. To test our Money implementation we therefore define MoneyTest as a subclass of TestCase. Put MoneyTest in the same package as the classes under test. In this way a test case has access to the package private methods. We add a test method testSimpleAdd, that will exercise the simple version of Money.add() above. A JUnit test method is an ordinary method without any parameters.

9 public class MoneyTest extends TestCase { //… public void testSimpleAdd() { Money m12CHF= new Money(12, "CHF"); // (1) Money m14CHF= new Money(14, "CHF"); Money expected= new Money(26, "CHF"); Money result= m12CHF.add(m14CHF); // (2) Assert.assertTrue(expected.equals(result)); // (3) } The testSimpleAdd() test case consists of: 1.Code which creates the objects we will interact with during the test. This testing context is commonly referred to as a test's fixture. All we need for the testSimpleAdd test are some Money objects. 2.Code which exercises the objects in the fixture. 3.Code which verifies the result.

10 A test for equals in MoneyTest. public void testEquals() { Money m12CHF= new Money(12, "CHF"); Money m14CHF= new Money(14, "CHF"); Assert.assertTrue(!m12CHF.equals(null)); Assert.assertEquals(m12CHF, m12CHF); Assert.assertEquals(m12CHF, new Money(12, "CHF")); // (1) Assert.assertTrue(!m12CHF.equals(m14CHF)); }

11 With JUnit you can have common fixture for running the tests by storing the fixture's objects in instance variables of your TestCase subclass and initialize them by overridding the setUp method. The symmetric operation to setUp is tearDown which you can override to clean up the test fixture at the end of a test. Each test runs in its own fixture and JUnit calls setUp and tearDown for each test so that there can be no side effects among test runs. public class MoneyTest extends TestCase { private Money f12CHF; private Money f14CHF; protected void setUp() { f12CHF= new Money(12, "CHF"); f14CHF= new Money(14, "CHF"); }

12 We can rewrite the two test case methods, removing the common setup code: public void testEquals() { Assert.assertTrue(!f12CHF.equals(null)); Assert.assertEquals(f12CHF, f12CHF); Assert.assertEquals(f12CHF, new Money(12, "CHF")); Assert.assertTrue(!f12CHF.equals(f14CHF)); } public void testSimpleAdd() { Money expected= new Money(26, "CHF"); Money result= f12CHF.add(f14CHF); Assert.assertTrue(expected.equals(result)); }

13 Two additional steps are needed to run the two test cases: 1.define how to run an individual test case, 2.define how to run a test suite. JUnit supports two ways of running single tests: static dynamic In the static way you override the runTest method inherited from TestCase and call the desired test case. A convenient way to do this is with an anonymous inner class. Note that each test must be given a name to identify it if it fails. TestCase test= new MoneyTest("simple add") { public void runTest() { testSimpleAdd(); } }; A template method in the superclass will make sure runTest is executed when the time comes.

14 The dynamic way to create a test case to be run uses reflection to implement runTest. –It assumes the name of the test is the name of the test case method to invoke. –It dynamically finds and invokes the test method. To invoke the testSimpleAdd test we therefore construct a MoneyTest as shown below: TestCase test= new MoneyTest("testSimpleAdd"); The dynamic way is more compact to write but it is less static type safe. –An error in the name of the test case goes unnoticed until you run it and get a NoSuchMethodException.

15 As the last step to getting both test cases to run together, we have to define a test suite. In JUnit this requires the definition of a static method called suite. The suite method is like a main method that is specialized to run tests. Inside suite you add the tests to be run to a TestSuite object and return it. A TestSuite can run a collection of tests. TestSuite and TestCase both implement an interface called Test which defines the methods to run a test. This enables the creation of test suites by composing arbitrary TestCases and TestSuites. public static Test suite() { TestSuite suite= new TestSuite(); suite.addTest(new MoneyTest("testEquals")); suite.addTest(new MoneyTest("testSimpleAdd")); return suite; }

16 there is an even simpler dynamic way. You only pass the class with the tests to a TestSuite and it extracts the test methods automatically. public static Test suite() { return new TestSuite(MoneyTest.class); } Here is the corresponding code using the static way. public static Test suite() { TestSuite suite= new TestSuite(); suite.addTest( new MoneyTest("money equals") { protected void runTest() { testEquals(); } } ); suite.addTest( new MoneyTest("simple add") { protected void runTest() { testSimpleAdd(); } } ); return suite; }

17 JUnit comes with a graphical interface to run tests. Type the name of your test class in the field at the top of the window. Press the Run button. While the test is run JUnit shows its progress with a progress bar below the input field. The bar is initially green but turns into red as soon as there is an unsuccessful test. Failed tests are shown in a list at the bottom.

18 Testing practice "Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead.“ What you want is to write tests that fail even though you think they should work, or tests that succeed even though you think they should fail. During Development- When you need to add new functionality to the system, write the tests first. Then, you will be done developing when the test runs. During Debugging- When someone discovers a defect in your code, first write a test that will succeed if the code is working. Then debug until the test succeeds.

19 Word of caution Once you get them running, make sure they stay running. There is a huge difference between having your suite running and having it broken. Ideally, you would run every test in your suite every time you change a method. Practically, your suite will soon grow too large to run all the time. Try to optimize your setup code so you can run all the tests. Or, at the very least, create special suites that contain all the tests that might possibly be affected by your current development. Then, run the suite every time you compile. And make sure you run every test at least once a day: overnight, during lunch

20 Ant Ant's buildfiles are written in XML. Each buildfile contains one project and at least one (default) target. Targets contain task elements. Each task element of the buildfile can have an id attribute and can later be referred to by the value supplied to this. The value has to be unique.

21 Project A project has three attributes: –name the name of the project. –default the default target to use when no target is supplied. –basedir the base directory from which all path calculations are done. This attribute might be overridden by setting the "basedir" property beforehand. If neither the attribute nor the property have been set, the parent directory of the buildfile will be used.

22 Target Each project defines one or more targets. A target is a set of tasks you want to be executed. When starting Ant, you can select which target(s) you want to have executed. When no target is given, the project's default is used.

23 Target Ant tries to execute the targets in the depends attribute in the order they appear (from left to right). It is possible that a target can get executed earlier when an earlier target depends on it: Each target gets executed only once, even when more than one target depends on it.

24 AttributeDescriptionRequired namethe name of the target.yes dependsa comma-separated list of names of targets on which this target depends. No ifthe name of the property that must be set in order for this target to execute. No unlessthe name of the property that must not be set in order for this target to execute. No descriptiona short description of this target's function.no

25 Task A task is a piece of code that can be executed. A task can have multiple attributes (or arguments, if you prefer). The value of an attribute might contain references to a property. These references will be resolved before the task is executed. Tasks have a common structure: where name is the name of the task, attributeN is the attribute name, and valueN is the value for this attribute. Tasks can be assigned an id attribute: where taskname is the name of the task, and taskID is a unique identifier for this task. You can refer to the corresponding task object in scripts or other tasks via this name.

26 Properties A project can have a set of properties. These might be set in the buildfile by the property task, or might be set outside Ant. A property has a name and a value; the name is case-sensitive. Properties may be used in the value of task attributes. This is done by placing the property name between " ${ " and " } " in the attribute value. For example, if there is a "builddir" property with the value "build", then this could be used in an attribute like this: ${builddir}/classes. This is resolved at run-time as build/classes.

27 Built-in properties Ant provides access to all system properties as if they had been defined using a task. For example, ${os.name} expands to the name of the operating system. In addition, Ant has some built-in properties: –basedir the absolute path of the project's basedir (as set with the basedir attribute of ). –ant.file the absolute path of the buildfile. –ant.version the version of Ant ant.project.name the name of the project that is currently executing; it is set in the name attribute of. –ant.java.version the JVM version Ant detected; currently it can hold the values "1.1", "1.2", "1.3", "1.4" and "1.5".

28 simple example build file

29

30 Path-like Structures You can specify PATH - and CLASSPATH -type references using both " : " and " ; " as separator characters. Ant will convert the separator to the correct character of the current operating system. Wherever path-like values need to be specified, a nested element can be used. This takes the general form of: The location attribute specifies a single file or directory relative to the project's base directory (or an absolute filename), while the path attribute accepts colon- or semicolon-separated lists of locations.

31 In addition, DirSets, FileSets, and FileLists can be specified via nested,, and elements, respectively.

32 If you want to use the same path-like structure for several tasks, you can define them with a element at the same level as targets, and reference them via their id attribute. A path-like structure can include a reference to another path-like structure via nested elements:

33 References Any project element can be assigned an identifier using its id attribute. In most cases the element can subsequently be referenced by specifying the refid attribute on an element of the same type. This can be useful if you are going to replicate the same snippet of XML over and over again--using a structure more than once.

34

35


Download ppt "Testing and Build JUnit and Ant. JUnit Testing is not closely integrated with development. This prevents you from measuring the progress of development-"

Similar presentations


Ads by Google