Difference between revisions of "6269 Programmering Java II Agenda/unittesting"
(Created page with "=Hvad er unit tests= Unit tests er et stykke kode der er skrevet for at teste en specifik funktionalitet i den kode der bliver testet. Procentdelen af den kode der bliver testet...") |
|||
Line 14: | Line 14: | ||
[https://www.jetbrains.com/idea/help/configuring-testing-libraries.html https://www.jetbrains.com/idea/help/configuring-testing-libraries.html] | [https://www.jetbrains.com/idea/help/configuring-testing-libraries.html https://www.jetbrains.com/idea/help/configuring-testing-libraries.html] | ||
+ | |||
+ | =Eksempel= | ||
+ | |||
+ | <source lang=csharp> | ||
+ | package dk.mercantec.micn.app; | ||
+ | |||
+ | public class Calculator { | ||
+ | public float add(float firstNumber, float lastNumber) { | ||
+ | return firstNumber + lastNumber; | ||
+ | } | ||
+ | |||
+ | public float sub(float firstNumber, float lastNumber) { | ||
+ | return firstNumber - lastNumber; | ||
+ | } | ||
+ | |||
+ | public float multiply(float firstNumber, float lastNumber) { | ||
+ | return firstNumber * lastNumber; | ||
+ | } | ||
+ | |||
+ | public float divide(float firstNumber, float lastNumber) { | ||
+ | if (lastNumber == 0) { | ||
+ | throw new IllegalArgumentException("You can't divide by zero!"); | ||
+ | } | ||
+ | return firstNumber / lastNumber; | ||
+ | } | ||
+ | |||
+ | public boolean isPositive(float number) { | ||
+ | return number > 0; | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | <source lang=csharp> | ||
+ | package dk.mercantec.micn.tests; | ||
+ | |||
+ | import dk.mercantec.micn.app.Calculator; | ||
+ | import org.junit.Test; | ||
+ | |||
+ | import static org.junit.Assert.*; | ||
+ | |||
+ | public class CalculatorTest { | ||
+ | |||
+ | @Test | ||
+ | public void testAdd() throws Exception { | ||
+ | Calculator calc = new Calculator(); | ||
+ | |||
+ | assertEquals(4f, calc.add(2f, 2f), 0f); | ||
+ | } | ||
+ | |||
+ | @Test | ||
+ | public void testSub() throws Exception { | ||
+ | Calculator calc = new Calculator(); | ||
+ | |||
+ | assertEquals(0f, calc.sub(2f, 2f), 0f); | ||
+ | } | ||
+ | |||
+ | @Test | ||
+ | public void testMultiply() throws Exception { | ||
+ | Calculator calc = new Calculator(); | ||
+ | |||
+ | assertEquals(4f, calc.multiply(2f, 2f), 0f); | ||
+ | } | ||
+ | |||
+ | @Test | ||
+ | public void testDivide() throws Exception { | ||
+ | Calculator calc = new Calculator(); | ||
+ | |||
+ | assertEquals(1f, calc.divide(2f, 2f), 0f); | ||
+ | } | ||
+ | |||
+ | @Test(expected=IllegalArgumentException.class) | ||
+ | public void testDivideByZero() throws Exception { | ||
+ | Calculator calc = new Calculator(); | ||
+ | |||
+ | assertEquals(1f, calc.divide(2f, 0f), 0f); | ||
+ | } | ||
+ | |||
+ | @Test | ||
+ | public void testIsPositive() throws Exception { | ||
+ | Calculator calc = new Calculator(); | ||
+ | |||
+ | assertTrue(calc.isPositive(2)); | ||
+ | } | ||
+ | } | ||
+ | </source> |
Latest revision as of 12:52, 22 February 2016
Hvad er unit tests
Unit tests er et stykke kode der er skrevet for at teste en specifik funktionalitet i den kode der bliver testet. Procentdelen af den kode der bliver testet bliver typisk kaldt test coverage (test dækning)
En unit test dækker en lille del af koden, f.eks. en metode eller en klasse.
Unit tests sikre at koden virker efter hensigten. De er også meget nyttige til at sikre at koden stadig virker efter hensigten efter at man har lavet ændringer for at rette en fejl eller tilføjet ny funktionalitet. En høj testdækning gør det muligt hurtigt udvikle nye features da man slipper for at lave en masse manuelle tests.
Hvad skal testes
Dette spørgsmål fører ofte til ophedede diskussioner blandt udviklere. Nogle mener at man skal have en testdækning på 100%, andre mener at det er nok at man bare dækker programmets core funktionalitet. Det mest realistiske er nok et sted midt imellem. Det giver f.eks. ingen mening at teste simple settere og gettere.
Opsætning
https://www.jetbrains.com/idea/help/configuring-testing-libraries.html
Eksempel
package dk.mercantec.micn.app;
public class Calculator {
public float add(float firstNumber, float lastNumber) {
return firstNumber + lastNumber;
}
public float sub(float firstNumber, float lastNumber) {
return firstNumber - lastNumber;
}
public float multiply(float firstNumber, float lastNumber) {
return firstNumber * lastNumber;
}
public float divide(float firstNumber, float lastNumber) {
if (lastNumber == 0) {
throw new IllegalArgumentException("You can't divide by zero!");
}
return firstNumber / lastNumber;
}
public boolean isPositive(float number) {
return number > 0;
}
}
package dk.mercantec.micn.tests;
import dk.mercantec.micn.app.Calculator;
import org.junit.Test;
import static org.junit.Assert.*;
public class CalculatorTest {
@Test
public void testAdd() throws Exception {
Calculator calc = new Calculator();
assertEquals(4f, calc.add(2f, 2f), 0f);
}
@Test
public void testSub() throws Exception {
Calculator calc = new Calculator();
assertEquals(0f, calc.sub(2f, 2f), 0f);
}
@Test
public void testMultiply() throws Exception {
Calculator calc = new Calculator();
assertEquals(4f, calc.multiply(2f, 2f), 0f);
}
@Test
public void testDivide() throws Exception {
Calculator calc = new Calculator();
assertEquals(1f, calc.divide(2f, 2f), 0f);
}
@Test(expected=IllegalArgumentException.class)
public void testDivideByZero() throws Exception {
Calculator calc = new Calculator();
assertEquals(1f, calc.divide(2f, 0f), 0f);
}
@Test
public void testIsPositive() throws Exception {
Calculator calc = new Calculator();
assertTrue(calc.isPositive(2));
}
}