EE308FZ LAB 11 Software Testing-Junit Test

Hongze_Pan_19103042 Hongze_Pan_19103042 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZ
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/603748107
The Aim of This AssignmentSoftware Testing-Junit Test
MU STU ID and FZU STU ID19103042_831902207

 

 

目录

Preparation

Task

Requirements

Tips:

Junit Test

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.

Tips:

 

 

@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.

 

 

  • We list ‘related knowledge’ for some problems, read it very carefully.

    Junit Test

  • 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.

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

     

  • Junit time test

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

     

  • Junit parameterized test

  • Related knowledge:Junit parameterized test allows you test the same test with different parameters. Learning Junit parameterized test with five steps bellow.

  • Annotate test class with @RunWith(Parameterized.class).
  • Build a static method annotated by @Parameters, which retrun a set or an array of Objects as test data.
  • Build a public construct method, which receives a parameter equals with test data.
  • Build instance variable for every colum of test data.
  • Build your test case with the instance as test data source.
  • 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;  
        }  
    }

     

  • Junit Exception Test

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

     

  • Junit Suite Test

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

     

...全文

相关推荐

《模式识别与数据挖掘》配套PPT课件

《模式识别与数据挖掘》配套PPT课件

Bryan Ding

科技成果转化项目进场交易前,评估备案环节需要准备哪些材料?_8.docx

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

BIZKEEN

人工智能通识教育(中职版)课程标准+PPT课件+部分素材+电子活页+在线考核

人工智能通识教育(中职版)课程标准+PPT课件+部分素材+电子活页+在线考核

Bryan Ding

跨境电商供应链管理(AIGC版)课件+教案+参考答案

跨境电商供应链管理(AIGC版)课件+教案+参考答案

Bryan Ding

110KV单电源电网三段式电流保护+距离继电保护整定计算说明报告(23页+6000字)

内容概要:本文是一份关于110kV单电源电网继电保护整定计算的综合性技术报告,全文共23页、约6000字,系统阐述了三段式电流保护与距离保护的设计原理及其整定计算方法。报告深入分析了电力系统在短路故障条件下的电流与阻抗特性,详细推导了电流保护Ⅰ、Ⅱ、Ⅲ段的动作范围、整定原则、灵敏度校验及时间配合机制,确保满足继电保护的选择性、速动性、灵敏性和可靠性“四性”要求。同时,针对长距离或重要输电线路,补充介绍了距离保护的阻抗整定计算方法,并可能结合Matlab进行仿真验证与辅助计算,提升整定结果的准确性与工程实用性。; 适合人群:适用于电气工程及其自动化相关专业的高年级本科生、研究生,以及从事电力系统继电保护设计、运行维护的工程技术人员;要求读者具备电力系统分析、短路计算及继电保护的基础理论知识。; 使用场景及目标:①掌握110kV单电源辐射型电网中三段式电流保护的整定流程与实际计算步骤;②理解距离保护的工作原理及其在复杂工况下的应用优势;③完成实际工程项目的保护配置与整定计算任务,或作为课程设计、毕业设计及科研课题的技术参考资料;④结合Matlab工具实现整定计算的程序化处理与动作特性仿真验证。; 阅读建议:建议读者结合《电力系统继电保护原理》等相关教材同步学习,重点研读整定公式的推导过程与典型算例的计算细节,注意区分电流保护与距离保护的适用场景与配置逻辑,有条件时应利用Matlab等工具对保护动作行为进行建模与仿真,以深化对保护协调配合机制的理解。

欢多天天写代码
评论
收藏 已收藏
分享
返回顶部