全国大学生软件测试开发者测试大赛笔记总结

全国大学生软件测试开发者测试大赛笔记总结

(1)常用头文件
import static org.junit.Assert.assertEquals;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
(2)抛出异常
//(一)(每一个测试方法都要写),2.用for循环赋值
@Test(timeout = 4000)
  public void test ()  throws Throwable  {
	  
  }
(3)暴力赋值
for实现大量赋值,可以保证判真判假都至少出现一次
for(int i=0;i<10;++i) {
          HashSet<Integer> hs = new HashSet<Integer>();
	      for(int j=0+i;j<10+i;++j){
	    	  hs.add(j);
	      }
	      nb.learn(i, hs);
      }
(4)浮点数
**************************************************
//浮点数测试第三个参数为精度:assertEquals(1.0, double0, 0.01);

(5)关于转义字符
//关于转义字符要加上:'\' ==>>Jipa.removeComment(";111()2;\"2");
//java中\s表示空白符,输入时要加\,   \\s

(6)关于集合
//关于集合
	HashMap<String,Integer> hashMap =new HashMap<String, Integer>();
	List<?> l = new ArrayList<>();//函数返回List<>用新的对象接收之后断言list.size
	List<?> l = new LinkedList<>();
	Map<String, Integer> m = new HashMap<>();
	Collection<Integer>c=new ArrayList<Integer>();
	Collection<Integer> c = Arrays.asList(10, 9, 7, 11, 12);
	Set<Map.Entry<K,V>>entry=new Map<String,ClassRoom>().entrySet();
	Map.entrySet();
(7)容器
Iterator<String> i = set.iterator();
	String st = i.next();
	Iterator迭代器hasNext() 
	如果仍有元素可以迭代,则返回 truenext()
(8)关于图
//关于图
Graph类中有Vertex静态内部类:
	final Graph.Vertex<Integer> v1 = new Graph.Vertex<Integer>(1);
Vertex:顶点
edge:边
先构造顶点,图Graph加入顶点。构造边,图Graph加入边。图加入顶点和边。

(9)常用断言方法
assertFalse()
	assertTrue()
	assertNull()
	assertNotNull()
	assertEquals(a,b);//谁在右边谁就是but
(10)测试private
//(一)测试private
public class Test {
	private static Class class1;
	private static 所要测试的类名 v;
	@BeforeClass
	public static void beforeClass() throws Exception{
		class1=Class.forName("net.mooctest.Variable");//类的位置
	}
	//测试类的私有构造函数
	@Test
	public void test()throws Exception{
		Constructor c=class1.getDeclaredConstructor(String.class);
		c.setAccessible(true);
		类名 v=(类名)c.newInstance("对应类型参数");
	}
	//测试类的私有有参方法
	@Test
	public void test1()throws Exception{
	
		Method method=class1.getDeclaredMethod("方法名",String.class);//String.class为参数
		method.setAccessible(true);//设置为可以访问私有函数
		method.invoke(v, "taohao");//对方法传参
	}
	@AfterClass
	public static void afterClass(){
		System.out.println("===测试完成将对象赋值为null===");
	}
	  
}
(11)测试异常字符串
@Rule
public ExpectedException expectedException = ExpectedException.none();
    @Test(timeout = 4000)
    public void test () {
        expectedException.expect(IllegalArgumentException.class);
		expectedException.expectMessage("The length of product's name cannot longer than 20: ");
		//写方法,构造对象 ,上一条语句就是测试这一句的异常输出
}
(12)测试异常
try{
	
	
}catch{ 
	assertEquals(FileNotFoundException.class,e.getClass());
}
(13)Scanner测试
//Scanner测试
//待测1
public static int readValue()
    {
        Scanner in = new Scanner(System.in);
        String tmpInt = in.nextLine();
        if (isNumeric(tmpInt))
            return Integer.parseInt(tmpInt);
        else
            return 0;
    }

//待测2
public static String getFileName()
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the full path to the file or type q to quit :");
        return in.nextLine();
    }

//这种需要输入的不用从键盘输入
//需要这种测试:
@Test(timeout = 4000)
	  public void test()  throws Throwable  {
		String data = "ss";//固定
		int s;//根据待测方法返回值类型
		InputStream stdin = System.in;//固定
		try { 
		System.setIn(new ByteArrayInputStream(data.getBytes())); //固定
		s=Jipa.readValue();//可变(待测方法)
		  } finally{ 
		System.setIn(stdin); //固定
		  }
		 assertEquals(0,s);

	  }


(14)输出测试
//System.out.println输出测试
public class WelcomePageTest {
    private PrintStream console = null;
    private ByteArrayOutputStream bytes = null;
 
    @Before
    public void setUp() {
        bytes = new ByteArrayOutputStream();
        console = System.out;// 获取System.out 输出流的句柄
        System.setOut(new PrintStream(bytes));//将原本输出到控制台Console的字符流重定向到bytes
 
    }
    @After
    public void tearDown() {
        System.setOut(console);
    }
 
    @Test
    public void test() {
        String massage= "Welocme to Test.";//为构造函数建立参数
       WelcomePage welcomePage = new WelcomePage(massage);//创建类的对象
        welcomePage.showWelcomeMessages();//调用函数
        String expected = new String("Welocme to Test.");
        assertEquals(expected,bytes.toString().trim().replace("\r",""));
        //"\r\n", windows系统的换行方式
    }
}
(15)对于一般测试出现超时
//对于一般测试出现超时:
@Test(timeout = 4000)
	  public void Test()  throws Throwable  {
		  String data = "2222222";
		  InputStream stdin = System.in;
	try { 
		System.setIn(new ByteArrayInputStream(data.getBytes()));
		//在这里写测试语句,调用函数
	      }finally{
			System.setIn(stdin);
	      }
	  }
(16)空参数
Helper.setDifference((char[]) null, (char[]) null);

(17)测试文件
//测试文件,文件放在这个里面
String fp = "src/main/resources/net/mooctest/demo.txt";
//向文件中写内容:一
	try {
            FileWriter writer=new FileWriter(fp);
            writer.write("abccba\n");
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
//向文件中写内容:二
String demo = Anagram.class.getResource("demo.txt").getPath();
File df = new File(demo);
BufferedWriter out= new BufferedWriter(new FileWriter(df));//或者BufferedWriter out = null;  
	  try {
			out = new BufferedWriter(new FileWriter(df));
		} catch (IOException e) {
			e.printStackTrace();
		}
      try {
  		out.write("brain\n");
		out.write("fuck\n");
		out.write("hello\n");
		out.write("hi\n");
		out.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
/
	try {
            FileWriter writer=new FileWriter(fp);
            writer.write("abccba\n");
            writer.write("\n");
            writer.write("abccba\n");
            writer.write("abccba\n");
            writer.write("abccba\n");
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
(18)assertEquals报错
assertEquals报错
expected:<...?:false Directory:[]>
but was:<...?:false Directory:[ {}]> 
不是少了[]{},而是少了空格和{}。

expected:<...?:false Directory:[]{}>
 but was:<...?:false Directory:[ ]{}> 
少了空格

你可能感兴趣的:(Java,java,软件测试)