Введение. Unit тестирование. (Тема 1.4)

Слайд 2

Unit тестирование

Unit тестирование

Слайд 3

Библиотека JUnit


Библиотека JUnit

Слайд 4

JUnit

JUnit

Слайд 5

Загрузка JUnit

Загрузка JUnit

Слайд 6

Структура директорий

Структура директорий

Слайд 7

JUnit

JUnit

Слайд 8

Библиотека “Калькулятор!” package org.cud.calc; public class Calculator { public int sum(int...

Библиотека “Калькулятор!”

package org.cud.calc;
public class Calculator {
public int sum(int... numbers) {

int total = 0;
for (int i : numbers) {
total += i;
}
return total;
}
}
Слайд 9

Компиляция библиотеки I:\>dir src\org\cud\calc Volume in drive I has no label.

Компиляция библиотеки

I:\>dir src\org\cud\calc
Volume in drive I has no label.
Volume

Serial Number is 8009-8B63
Directory of I:\src\org\cud\calc
01/13/2013 12:56 PM .
01/13/2013 12:56 PM ..
11/14/2012 05:00 PM 185 Calculator.java
1 File(s) 185 bytes
2 Dir(s) 237,193,134,080 bytes free
I:\>javac -d bin src\org\cud\calc\Calculator.java
I:\>dir bin\org\cud\calc
Volume in drive I has no label.
Volume Serial Number is 8009-8B63
Directory of I:\bin\org\cud\calc
01/13/2013 12:56 PM .
01/13/2013 12:56 PM ..
01/13/2013 05:40 PM 369 Calculator.class
1 File(s) 369 bytes
2 Dir(s) 237,193,134,080 bytes free
I:\>
Слайд 10

Тест package org.cud.calc; import static org.junit.Assert.assertEquals; import org.junit.Test; public class CalculatorTest

Тест

package org.cud.calc;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CalculatorTest {
@Test
public void

test() {
Calculator calculator = new Calculator();
assertEquals(9, calculator.sum(2, 3, 4));
}
}
Слайд 11

Компиляция теста I:\>dir test\org\cud\calc Volume in drive I has no label.

Компиляция теста

I:\>dir test\org\cud\calc
Volume in drive I has no label.
Volume

Serial Number is 8009-8B63
Directory of I:\test\org\cud\calc
01/13/2013 05:50 PM .
01/13/2013 05:50 PM ..
11/14/2012 05:21 PM 256 CalculatorTest.java
1 File(s) 256 bytes
2 Dir(s) 237,193,125,888 bytes free
I:\>javac -d test_bin -cp lib/junit-4.8.2.jar;bin test\org\cud\calc\CalculatorTest.java
I:\>dir test_bin\org\cud\calc
Volume in drive I has no label.
Volume Serial Number is 8009-8B63
Directory of I:\test_bin\org\cud\calc
01/13/2013 06:42 PM .
01/13/2013 06:42 PM ..
01/13/2013 06:42 PM 484 CalculatorTest.class
1 File(s) 484 bytes
2 Dir(s) 237,193,125,888 bytes free
I:\>
Слайд 12

Запуск теста I:\>java -cp lib/junit-4.8.2.jar;test_bin;bin org.junit.runner.JUnitCore org.cud.calc.CalculatorTest JUnit version 4.8.2 .

Запуск теста

I:\>java -cp lib/junit-4.8.2.jar;test_bin;bin org.junit.runner.JUnitCore org.cud.calc.CalculatorTest
JUnit version 4.8.2
.
Time: 0
OK (1 test)
I:\>

Слайд 13

Параметризованный тест package org.cud.calc; import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith;

Параметризованный тест

package org.cud.calc;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameters;
import java.util.Arrays;
import java.util.Collection;
@RunWith(value =

org.junit.runners.Parameterized.class)
public class CalculatorParamTest {
int result;
int[] numbers;
@Parameters
public static Collection parameters() {
return Arrays.asList(new int[][][] { { { 2 }, { 1, 1 } },
{ { -2 }, { -1, -1 } }, { { 9 }, { 2, 3, 4 } }, { { 0 }, {} },
{ { 0 }, { 0, 0, 0, 0 } } });
}
public CalculatorParamTest(int[] result, int[] numbers) {
this.result = result[0];
this.numbers = numbers;
}
@Test
public void testSum() {
Calculator calculator = new Calculator();
assertEquals(result, calculator.sum(numbers));
}
}
Слайд 14

Компиляция параметризованного теста I:\>dir test\org\cud\calc Volume in drive I has no

Компиляция параметризованного теста

I:\>dir test\org\cud\calc
Volume in drive I has no label.

Volume Serial Number is 8009-8B63
Directory of I:\test\org\cud\calc
01/13/2013 07:16 PM .
01/13/2013 07:16 PM ..
11/14/2012 05:32 PM 881 CalculatorParamTest.java
1 File(s) 881 bytes
2 Dir(s) 237,193,134,080 bytes free
I:\>javac -d test_bin -classpath lib/junit-4.8.2.jar;bin test\org\cud\calc\CalculatorParamTest.java
I:\>dir test_bin\org\cud\calc
Volume in drive I has no label.
Volume Serial Number is 8009-8B63
Directory of I:\test_bin\org\cud\calc
01/13/2013 07:46 PM .
01/13/2013 07:46 PM ..
01/13/2013 07:46 PM 1,247 CalculatorParamTest.class
1 File(s) 1,247 bytes
2 Dir(s) 237,193,113,600 bytes free
I:\>