软件测试 junit

Posted Jerma6

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了软件测试 junit相关的知识,希望对你有一定的参考价值。

github 项目地址

https://github.com/software-Testing-1506/softwareTesting

PSP表格填写

接口的实现

个人负责输出接口以及文件操作及文件名筛选部分的实现,为达到松散耦合的编码目的,我们小组对数据进行封装然后在不同接口间传输。

主要思路

  • 在分析参数的过程中,将输出信息与文件句柄存在一个结构体里,并将可能存在的多个结构体存到一个数组里,由print函数接收并解析数组,依次根据句柄打开文件并写入信息。
  • 将要输出到屏幕上的信息也包成数组送到print函数中,再进行统一输出
  • 解析文件名,重载TraversalDir()处理打开多个文件可能有后缀的情况,如果有后缀,就使用过滤器filter,过滤掉不符合条件的文件

屏幕输出

文件输出

解析文件名

测试用例的设计

测试原理

白盒测试
白盒测试的测试方法有代码检查法、静态结构分析法、静态质量度量法、逻辑覆盖法、基本路径测试法、域测试、符号测试、路径覆盖和程序变异。 白盒测试法的覆盖标准有逻辑覆盖、循环覆盖和基本路径测试。其中逻辑覆盖包括语句覆盖、判定覆盖、条件覆盖、判定/条件覆盖、条件组合覆盖和路径覆盖。六种覆盖标准发现错误的能力呈由弱到强的变化:
  • 语句覆盖每条语句至少执行一次。
  • 判定覆盖每个判定的每个分支至少执行一次。
  • 条件覆盖每个判定的每个条件应取到各种可能的值。
  • 判定/条件覆盖同时满足判定覆盖条件覆盖。
  • 条件组合覆盖每个判定中各条件的每一种组合至少出现一次。
  • 路径覆盖使程序中每一条可能的路径至少执行一次。

然而遗憾的是我负责的部分对白盒测试 不太适合
为了学习, 我特意去测试了一下输入控制模块 如下:

黑盒测试

对于输出可暂且划为四个等价类

全部用例代码

@Test
public void testPrint1() {
	String s = "asdf";
	String filename = "output.txt";
	ArrayList<String> as = new ArrayList<String>();
	as.add(s);
	ArrayList<outfile> ao = new ArrayList<outfile>();
	ao.add(new outfile(new File(filename),s));
	javarestore.print(as,ao);
	Assert.assertEquals("output to files but the content of files not match", javarestore.readFileByChars(filename), s);
}
@Test
public void testPrint2() {
	String s = "asdf";
	String filename = "output.txt";
	ArrayList<String> as = new ArrayList<String>();
	as.add(s);
	ArrayList<outfile> ao = new ArrayList<outfile>();
	ao.add(new outfile(new File(filename),s));
	javarestore.print(as,ao);
	Assert.assertEquals("output to files but the content of files not match", javarestore.readFileByChars(filename), s);
}
@Test
public void testPrint3() {
	String s = "asdf";			//simulate two files output
	String filename = "output.txt";
	ArrayList<String> as = new ArrayList<String>();
	ArrayList<outfile> ao = new ArrayList<outfile>();
	as.add(s);
	ao.add(new outfile(new File(filename),s));

	String s1 = "xzvbfwetyt";
	String filename1 = "output.tt";
	as.add(s1);
	ao.add(new outfile(new File(filename1),s1));
	javarestore.print(as,ao);
	Assert.assertEquals("output to files but the content of files not match", javarestore.readFileByChars(filename), s);
	Assert.assertEquals("output to files but the content of files not match", javarestore.readFileByChars(filename1), s1);
}

@Test
public void testPrint4() {
	String s = "asdf";			//simulate two files output
	String filename = "output.txt";
	ArrayList<String> as = new ArrayList<String>();
	ArrayList<outfile> ao = new ArrayList<outfile>();
	as.add(s);
	ao.add(new outfile(new File(filename),s));

	String s1 = "xzvbfwetyt";
	String filename1 = "output.tt";
	as.add(s1);
	ao.add(new outfile(new File(filename1),s1));
	
	String s2 = "safgsfghtr";
	as.add(s2);
	String filename2 = "junittest";
	ao.add(new outfile(new File(filename2),s2));
	javarestore.print(as,ao);
	Assert.assertEquals("output to files but the content of files not match", javarestore.readFileByChars(filename), s);
	Assert.assertEquals("output to files but the content of files not match", javarestore.readFileByChars(filename1), s1);	
	Assert.assertEquals("output to files but the content of files not match", javarestore.readFileByChars(filename2), s2);
}

@Test
public void testfileopen() {
	String filename = "output.txt";
	String testcontent = "it\'s a tricking content test checking if the function is right";
	try {
		javarestore.writeFileByChars(new File(filename), testcontent);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	Assert.assertEquals("not fit traversal dir filenames",javarestore.readFileByChars(filename),testcontent);
	
}
@Test
public void testTraversalDirWithSuffix1() {
	String filesuffix = ".cpp";
	String[] filenames = {"asd.cpp"};
	int count=0;
	Assert.assertEquals("not fit ames\'num",javarestore.TraversalDir(filesuffix).length,filenames.length);
	for(String s:javarestore.TraversalDir(filesuffix)) {
		Assert.assertEquals("not fit traversal dir filenames",s,filenames[count]);
		count+=1;
	}
}
@Test
public void testTraversalDirWithSuffix2() {
	String filesuffix = ".c";
	String[] filenames = {"a.c","x.c"};
	int count=0;
	Assert.assertEquals("not fit ames\'num",javarestore.TraversalDir(filesuffix).length,filenames.length);
	for(String s:javarestore.TraversalDir(filesuffix)) {
		Assert.assertEquals("not fit traversal dir filenames",s,filenames[count]);
		count+=1;
	}
}
@Test
public void testTraversalDirWithSuffix3() {
	String filesuffix = ".tt";
	String[] filenames = {"output.tt"};
	int count=0;
	Assert.assertEquals("not fit ames\'num",javarestore.TraversalDir(filesuffix).length,filenames.length);
	for(String s:javarestore.TraversalDir(filesuffix)) {
		Assert.assertEquals("not fit traversal dir filenames",s,filenames[count]);
		count+=1;
	}
}
@Test
public void testTraversalDirWithSuffix4() {
	String filesuffix = ".txt";
	String[] filenames = {"output.txt"};
	int count=0;
	Assert.assertEquals("not fit ames\'num",javarestore.TraversalDir(filesuffix).length,filenames.length);
	for(String s:javarestore.TraversalDir(filesuffix)) {
		Assert.assertEquals("not fit traversal dir filenames",s,filenames[count]);
		count+=1;
	}
}
@Test
public void testTraversalDirWithSuffix5() {
	String filesuffix = ".classpath";
	String[] filenames = {".classpath"};
	int count=0;
	Assert.assertEquals("not fit ames\'num",javarestore.TraversalDir(filesuffix).length,filenames.length);
	for(String s:javarestore.TraversalDir(filesuffix)) {
		Assert.assertEquals("not fit traversal dir filenames",s,filenames[count]);
		count+=1;
	}
}
@Test
public void testTraversalDir() {			//ide explain * as all the filenames but the 
	String[] filenames = {".classpath",".project","a.c","asd.cpp","checkword","javaforcednote","output","output.tt","output.txt","stoplist","text","x.c"};
	int count=0;
	Assert.assertEquals("not fit traversal dir filenames\'num",javarestore.TraversalDir().length,javarestore.TraversalDir().length);
	for(String s:javarestore.TraversalDir()) {
		Assert.assertEquals("not fit traversal dir filenames",s,filenames[count]);
		count+=1;
	}
}

测试用例清单

以上是关于软件测试 junit的主要内容,如果未能解决你的问题,请参考以下文章

junit4单元测试--web项目中模拟登录会话,做全流程测试

使用java.lang.Exception的错误:测试类应该只有一个公共构造函数

四则运算单元测试

通过JUnit进行Android单元测试

JUnit代码测试是啥?怎么写代码?

熟悉 JUnit 测试