EE308_LAB 11
| The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZ?category=0 |
|---|---|
|
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/603748107 |
| The Aim of This Assignment | Software Testing-Junit Test |
| MUID&FZUID | 19104715 & 831902214 |
Task
- Here are Junit Test task and Unittest task for choose. If you are good at Java , we suggest you choose the first one, if you are good at Python, then, the latter one.
- Follow the requests and tips, complete the test task .
1. Junit assert
Related Knowledge: you can check the expected result and the real result of the method you test with the help of org.junit.Assert.
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**************************/
assertEquals(obj1,obj2);
assertTrue(var1 < var2);
assertFalse(obj4==obj5);
assertNotNull(obj1);
assertNull(obj5);
assertSame(obj1,obj2);
assertNotSame(obj1,obj3);
assertArrayEquals(arithmetic1,arithmetic2);
/************************End***************************/
}
}
2.Junit time test
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() {
int i = 0;
while(true){
break; // if it is not break will failures
}
/************************End***************************/
}
}
3.Junit parameterized test
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;
/**
* 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********************************************/
Object[][] ages={{1,1,0},{1,2,-1},{2,1,1},{0,0,0}};
return Arrays.asList(ages);
/**********************************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;
}
}
4.Junit exception test
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
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);
}
}
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;
}
}
5. Junit suite test
package lab10_2;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/*
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 {
}
//-------------------------------------
package lab10_2;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
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);
}
}
//------------------------------------
package lab10_2;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
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);
}
}
//----------------------------
package lab10_2;
public class Calculate {
public int add(int a, int b) {
return a + b;
}
}
//--------------------------------
package lab10_2;
public class Car {
public int getWheels() {
return 4;
}
}
...全文
相关推荐
EE-308 LAB4 final version.7z
这是本次实验的压缩包
EE308-Lab11-Software Testing
unittest for Python
EE308-Lab8-AlphaSprint Collection
EE308 Lab8 Alpha Sprint Collection1. Format Description2. 宁可累死自己,也要卷死同学 -Sprint Collection 1. Format Description The Link Your Class https://bbs.csdn.net/forums/MUEE308FZ The Link of Requirement of This Assignment https://bbs.csdn.net/topics/603251
【信息科学与工程学】【物理/化学科学和工程技术】知识体系073——电学基础04
。
AI Design Skills一个不断壮大的设计技能合集,适用于 AI 编码工具
用户可借助此项目为 Claude Code、Cursor 等 AI 编码工具添加设计技能,轻松构建如着陆页等内容。项目包含多个独立设计技能文件夹,支持自定义设计值,安装方式简单适配多种工具


