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

科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。

BIZKEEN

【基于dq控制算法的并联有源滤波器】分流有源滤波器的仿真电路可以减轻谐波和无功功率(simulink仿真实现)

内容概要:本文详细介绍了基于dq控制算法的并联有源滤波器(APF)在Simulink环境下的仿真电路设计与实现过程,重点阐述了通过分流式拓扑结构有效抑制电力系统中的谐波污染并补偿无功功率的技术方案。文中深入剖析了dq变换的核心原理,将其应用于控制系统中,实现三相交流量到直流量的转换,从而简化控制器设计并提升动态响应精度。仿真模型完整构建了主电路、负载模块及控制策略三大组成部分,能够直观呈现滤波前后电流波形的改善效果,验证了该方法在提高电能质量方面的有效性与可行性。; 适合人群:电力电子、电气工程及其自动化等相关专业的高校师生,以及从事电能质量治理、有源滤波器研发及相关领域工程应用的技术人员。; 使用场景及目标:①用于课堂教学与课程设计,帮助学生理解dq变换机制及有源滤波器的工作原理;②为科研项目提供可复现的仿真平台,支持新型控制算法的开发与验证;③辅助企业在实际工程中优化APF产品设计,增强谐波抑制与无功补偿性能。; 阅读建议:建议结合MATLAB/Simulink环境动手搭建与调试模型,深入掌握各控制模块参数整定方法及其对系统性能的影响,可通过改变负载类型或引入非理想工况进行对比分析,以全面提升对系统稳态与动态特性的理解与掌控能力。

大龙不吃小鱼干

国央企创新负责人如何利用平台实现技术成果的精准对接与协同创新?.docx

科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。

BIZKEEN

中小功率逆变电源关键单元电路设计与保护策略研究【原理+PCB+程序】

内容概要:本文围绕中小功率逆变电源的关键单元电路设计与保护策略展开系统性研究,涵盖逆变电源的工作原理、硬件电路设计(包括DC-DC升压与DC-AC逆变)、SPWM正弦波调制技术、闭环控制策略(电压/电流双闭环)、系统保护机制(过压、过流、短路、过温等)以及基于STM32的嵌入式控制系统开发。研究内容整合了Simulink仿真模型、PCB设计文件与嵌入式程序代码,实现了从理论分析、仿真验证到硬件实现的完整技术链条,重点突出系统稳定性设计与多层级保护策略的协同优化,适用于逆变器产品的研发与工程实践。; 适合人群:具备电力电子、自动控制或电气工程等相关专业背景,从事电源系统设计的研发工程师、高校研究生及工程技术人员,尤其适合具有一定嵌入式系统开发经验(熟悉STM32系列单片机)且工作年限在1-5年的从业人员。; 使用场景及目标:①用于中小功率离网/并网逆变器的原型开发与产品设计;②作为高校电力电子、自动化等专业的课程设计、毕业设计或科研项目的综合性参考方案;③深入掌握SPWM调制、LC滤波器设计、PID闭环控制、系统保护逻辑等关键技术的实现方法;④结合仿真与实物平台进行波形测试与参数调试,提升电力电子系统的综合设计能力与故障排查水平。; 阅读建议:建议读者结合提供的原理图、PCB布局图与源代码同步学习,优先在Simulink中完成控制逻辑仿真,理解系统动态响应特性后再搭建实际硬件平台。重点关注保护电路的动作阈值设置与软硬件协同保护机制的实现方式,实验过程中应配合示波器、万用表等仪器观测关键节点波形,逐步优化控制参数以提升系统效率与稳定性。

程序猿鑫

vector_search_node.py

vector_search_node.py

it_xiaohua
评论
收藏 已收藏
分享
返回顶部