EE308 lab11

zzz 2021-12-16 21:46:51

The Link Your Class

 

https://bbs.csdn.net/forums/MUEE308FZ

The Link of Requirement of This Assignment

LAB 11 Software Testing-CSDN社区

The Aim of This Assignment

Software Testing

MU STU ID and FZU STU ID

19105886-831902204

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.

The following is the code I use to test:

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.

 

 

1.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**************************/
        assertTrue(var1 < var2);
        assertFalse(obj4==obj5);
        assertEquals(obj1,obj2);
        assertSame(obj1,obj2);
        assertNotSame(obj1,obj3);
        assertArrayEquals(arithmetic1,arithmetic2);
        assertNotNull(obj1);
        assertNull(obj5);
        //assertTrue(var1 < var2); for example
	    /************************End***************************/
        }
}
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;
        }
    }
    /************************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.

  1. Annotate test class with @RunWith(Parameterized.class).
  2. Build a static method annotated by @Parameters, which retrun a set or an array of Objects as test data.
  3. Build a public construct method, which receives a parameter equals with test data.
  4. Build instance variable for every colum of test data.
  5. 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

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********************************************/
        Object[][] ages={{2,2,0},{0,1,-1},{0,1,-1},{3,1,2}};
		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;  
    }  
}

 

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

 

 

...全文
129 回复 打赏 收藏 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
源码下载地址: https://pan.quark.cn/s/07e7935aff27 在Android应用开发过程中,`ImageView`作为一个基础组件被广泛运用,其主要功能是展示各种图像资源,这些资源可以来源于本地存储或通过网络获取。本文将详细阐述如何动态地变更`ImageView`所显示的图片内容,并借助自定义监听机制来监控和干预图片的加载流程。此类技术在应用开发中具有很高的实用价值,特别是在处理网络用户头像、商品展示图以及实时数据更新的场景下。 我们首先需要明确`ImageView`的基本操作方式。在Android平台,我们常通过`setImageResource()`、`setImageBitmap()`以及`setImageDrawable()`等函数来设定`ImageView`的图像内容。然而,这些方法主要适用于已加载至内存中的图像文件,对于网络图片,则需要借助更为复杂的处理逻辑。 在实现动态下载并更新`ImageView`中的图像时,我们普遍采用`AsyncTask`或`Loader`来执行后台任务,以此防止主线程被阻塞。本文将重点介绍自定义监听器的构建方法。自定义监听器能够让我们依据图片加载的进程或状态来执行特定的任务,例如显示加载进度指示器或处理加载失败的情况。 自定义监听器通常定义为一个接口,其中包含了若干个回调方法,例如`onStart()`、`onProgressUpdate()`、`onSuccess()`、`onError()`等。以下是一个示例接口定义: ```java public interface ImageDownloaderListener { void onStart(); void onProgress(int ...

183

社区成员

发帖
与我相关
我的任务
社区描述
福州大学 梅努斯国际工程学院 软件工程 教学
软件工程 高校
社区管理员
  • 单步调试
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧