183
社区成员




The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZ |
---|---|
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/603748107 |
The Aim of This Assignment | Software Testing-Junit Test |
MU STU ID and FZU STU ID | 19103042_831902207 |
目录
Learn related knowledge about Junit Test for Java and Unittest for python. You can finish the following assignment or test Your Project.
@Parameters:Add this annotation to every method if it provide data. By the way, these methods must be static, return a Collection and receive no parameter.
P.S.: You must assign value for every field in the class no matter used or unused!
In Junit, you can use @RunWith and @parameter to pass parameters.
@RunWith:When a class annotated by @RunWith or when a class extends a base class which annotated by @RunWith,then Junit will run test through a runner pointed by the annotation.
Related Knowledge: you can check the expected result and the real result of the method you test with the help of org.junit.Assert.
task:given a assert test class, complete assert test code
import static org.junit.Assert.*;
import org.junit.Test;
public class AssertionsTest {
String obj1 = "junit";
String obj2 = "junit";
String obj3 = "test";
String obj4 = "test";
String obj5 = null;
int var1 = 1;
int var2 = 2;
int[] arithmetic1 = { 1, 2, 3 };
int[] arithmetic2 = { 1, 2, 3 };
@Test
public void test() {
// add assert test code between Begin and End, no other change allowed
/***********************Begin**************************/
//assertTrue(var1 < var2); for example
assertEquals(“obj1不等于obj2”,obj1,obj2);
System.out.println(“obj1等于obj2”);
assertTrue(“var1 小于 var2 是”,var1<var2);
System.out.println(“var1 小于 var2 ”);
assertFalse(“var1 大于 var2 是”);
System.out.println(“var1 大于 var2 ” 错误);
assertNotNull(obj3);
System.out.println("object 不是 not null ");
assertNull(obj5);
System.out.println("obj5 is null");
assertArrayEquals(arithmetic1,arithmetic2);
System.out.println("arithmetic1 is equal to arithmetic2");
/************************End***************************/
}
}
if a test case takes longer time you set, then Junit will mark it failed.
tip: use ‘timeout’ and ‘@Test’ togeter
import org.junit.Test;
public class TestTimeOut {
// Fix timeout assert in Test function below. Test fail if running 1000ms longer
/***********************Begin**************************/
@Test(timeout = 1000)
public void test() {
while(true){}
}
/************************End***************************/
}
Related knowledge:Junit parameterized test allows you test the same test with different parameters. Learning Junit parameterized test with five steps bellow.
In Junit, you can use @RunWith and @parameter to pass parameters.
@RunWith:When a class annotated by @RunWith or when a class extends a base class which annotated by @RunWith,then Junit will run test through a runner pointed by the annotation.
@Parameters:Add this annotation to every method if it provide data. By the way, these methods must be static, return a Collection and receive no parameter.
P.S.: You must assign value for every field in the class no matter used or unused!
import static org.junit.Assert.assertEquals; // static import
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import step1.Calculator;
/**
* JUnit4 parameterized test
*/
@RunWith(Parameterized.class)
public class ParameterTest {
private int input11;
private int input22;
private int expected;
public ParameterTest(int input11, int input22, int expected){
this.input11 = input11;
this.input22 = input22;
this.expected = expected;
}
@Parameters
public static Collection prepareData(){
/**
*the type of the two-dimension array must be Object.
*data in the two-dimension array is ready of test sub() in Calculator
* every element in the two-dimension array should corresponds to position of parameters in construct method ParameterTest
*let the third element equals the first subtract the second element according to parameters’ postion
*fix missing codes under ‘Begin’ and above ‘End’,pass 4 groups of parameters to test sub method in Calculator is right or not
*tip:only two lines of codes
*/
/*********************************Begin****************************************/
return Arrays.asList(new.Object[][]);
new.Object[][] = { {1,1,1} , { 3,1,2},{2,2,2},{2,1,1}};
/**********************************End****************************************/
}
@Test
public void testSub(){
Calculator cal = new Calculator();
assertEquals(cal.sub(input11, input22), expected);
}
}
// Calculator.java Junit Parameterized Test
/**
* Mathematical Calculation subtract
*/
public class Calculator {
public int sub(int a, int b) {
return a - b;
}
}
Related knowledge:you can check codes if throw expected exception or not by using ‘expected’ attribute in @Test meta data. value of the ‘expected attribute’ is a kind of Exception, if codes throw the expected exception, then test successfully, otherwise, failed.
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import step2.Person;
public class JunitException {
/**
*add a line of annotation in Begin/End,check the age of Person Object is legal or not. *throw IllegalArgumentException exception
*/
/***********************************Begin***********************************/
@Test(expected=IllegalArgumentException.class)
/************************************End************************************/
public void checkage() {
Person person = new Person();
person.setAge(-1);
}
}
//Person.java
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age < 0 ) {
throw new IllegalArgumentException("age is invalid");
}
this.age = age;
}
}
Related knowledge:Suite Test means test a couple of test cases together. Precisely speaking, Using @RunWith and @Suite.
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import step3.Calculate;
import step3.CalculateTest;
import step3.Car;
import step3.CarTest;
/*
add two lines of annotations. Implement Suite Test of CalculateTest and CarTest
Suite Test codes must next to Class SuiteTest, no shift allowed!
*/
//**************************************************************
@RunWith(Suite.class)
@Suite.SuiteClasses({ CalculateTest.class, CarTest.class})
public class SuiteTest {
@Test
public void testPrint() {
System.out.println("The result of CalculateTest");
assertEquals(24, result);
System.out.println("The result of CarTest");
assertEquals(4, result);
}
}
//Calculate.java
public class Calculate {
public int add(int a, int b) {
return a + b;
}
}
//CalculateTest.java
public class CalculateTest {
Calculate calculate;
@Before
public void setUp() throws Exception {
calculate = new Calculate();
}
@Test
public void testAdd() {
int result = calculate.add(12, 12);
assertEquals(24, result);
}
}
//CarTest.java
public class CarTest {
Car car;
@Before
public void setUp() throws Exception {
car = new Car();
}
@Test
public void testGetWheels() {
int result = car.getWheels();
assertEquals(4, result);
}
}
//Car.java
public class Car {
public int getWheels() {
return 4;
}
}