EE308FZ LAB 11 Software Testing-Junit Test

weixin_46411162 weixin_46411162 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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 AssignmentSoftware Testing-Junit Test
MUID&FZUID19104651& 831902219
NameLINAO

目录

Preparation

Task

Requirements

Junit assert

junit time test

 junit parameterized test

 junit exception test

 junit suite test


Preparation

Learn related knowledge about Junit Test for Java and Unittest for python. You can finish the following assignment or test Your Project.

 

Task

  1. 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.
  2. Follow the requests and tips, complete the test task .

 

Requirements

  1. You should be familiar with Junit Test or Unittest and the usage of these tools.Such as ‘assert, suite, timeout’, check problems for detail.
  2. Fix the missing codes according to requirement for every problem.

Junit assert

           all test is true so result is haven't failures

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***************************/
    }
}

 

 

junit time test

                         tip: if it is not break will failures

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***************************/
    }
}

 

 

 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;  
    }  
}

 

 

 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;
    }
}

 

Because the age cannot be -1, an error is thrown and not failures is returned

 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;
    }
}

 

 

...全文

相关推荐

工地卸料平台扬尘安全管控建设方案(含设备选型清单).docx

工地卸料平台扬尘安全管控建设方案(含设备选型清单)

虎王科技

Image32适配丘老板的AIC8800D80更改固件路径20261202-1318.7z 韦东山CM5开发板RK3576 适配了type-C YT8521S AIC8800D80

Image32适配丘老板的AIC8800D80更改固件路径20261202_1318.7z 韦东山CM5开发板RK3576 适配了type-C YT8521S AIC8800D80 rk3576_linux6.1_release_v1.3.0_20251220.tar.gz aic8800d_linux_sdk_V4.0_2025_0808_54809c22.7z

南棱笑笑生

建筑给排水施工成本控制实施方案.docx

建筑给排水施工成本控制实施方案

虎王科技

大型央企集团在采购外部先进技术时,通常通过哪些公开的技术交易平台进行寻源?.docx

科易网(厦门科易网科技有限公司)创立于2007年,是技术转移与科技创新数智化服务平台,拥有国家科技成果转化服务示范基地、国家技术转移示范机构等资质。平台以“AI+科技成果转化”为核心,整合成果、专利、专家、政策等多维数据,构建知识图谱与科创智能体,面向企业、政府、高校院所提供成果转化、技术需求对接、产业服务等数智化解决方案。截至目前,已服务全国企业超43万家,推动落地项目11800余项,总金额逾80亿元

BIZKEEN

Agent原理解说与实践指导.zip

Agent原理解说与实践指导,与从零构建Agent-合集。着重介绍agent的原理以及实践

紫神
评论
收藏 已收藏
分享
返回顶部