JUnit单元测试覆盖率测试 - Printtokens2类白盒测试
JUnit单元测试覆盖率测试 - Printtokens2类白盒测试
本测试用例旨在对Printtokens2类进行白盒测试,使用JUnit框架编写测试代码,并生成HTML格式的代码覆盖率报告。
测试用例设计
根据白盒法,对Printtokens2类中的每个方法进行测试,测试用例涵盖了各种输入场景,确保覆盖所有代码分支和逻辑。
1. 测试open_character_stream方法
- a. fname为null
- b. fname为存在的文件名
- c. fname为不存在的文件名
2. 测试get_char方法
- a. BufferedReader中还有字符
- b. BufferedReader已经没有字符
3. 测试unget_char方法
- a. BufferedReader中还有字符
- b. BufferedReader已经没有字符
4. 测试open_token_stream方法
- a. fname为null
- b. fname为存在的文件名
- c. fname为不存在的文件名
5. 测试get_token方法
- a. 读取到特殊符号
- b. 读取到字符串
- c. 读取到注释
- d. 读取到普通标识符
- e. 读取到数值常量
- f. 读取到字符常量
- g. 读取到不符合规则的token
6. 测试is_token_end方法
- a. str_com_id为1,res为换行符或回车符或'"'
- b. str_com_id为2,res为换行符或回车符或制表符
- c. res为特殊符号
- d. res为空格或换行符或回车符或分号
7. 测试token_type方法
- a. token为关键字
- b. token为特殊符号
- c. token为标识符
- d. token为数值常量
- e. token为字符常量
- f. token为字符串
- g. token不符合规则
8. 测试print_token方法
- a. token为关键字
- b. token为特殊符号
- c. token为标识符
- d. token为数值常量
- e. token为字符常量
- f. token为字符串
- g. token不符合规则
9. 测试is_comment方法
- a. token为注释
- b. token不为注释
10. 测试is_keyword方法
- a. token为关键字
- b. token不为关键字
11. 测试is_char_constant方法
- a. token为字符常量
- b. token不为字符常量
12. 测试is_num_constant方法
- a. token为数值常量
- b. token不为数值常量
13. 测试is_str_constant方法
- a. token为字符串
- b. token不为字符串
14. 测试is_identifier方法
- a. token为标识符
- b. token不为标识符
15. 测试print_spec_symbol方法
- a. token为左括号
- b. token为右括号
- c. token为左方括号
- d. token为右方括号
- e. token为'"'
- f. token为反引号
- g. token为其他特殊符号
16. 测试is_spec_symbol方法
- a. token为左括号
- b. token为右括号
- c. token为左方括号
- d. token为右方括号
- e. token为斜杠
- f. token为反引号
- g. token为逗号
- h. token为其他字符
代码示例
// 测试用例代码示例
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class Printtokens2Test {
@Test
void testOpenCharacterStream() {
// 测试用例代码示例
}
// 其他测试用例代码示例
}
代码覆盖率报告
使用JUnit的代码覆盖率工具生成HTML格式的代码覆盖率报告,可以直观地了解测试用例覆盖的代码行数和分支情况,方便评估测试用例的有效性和代码质量。
结论
通过白盒测试,可以有效地测试代码的逻辑和功能,并保证代码的质量和可靠性。
附录
- Printtokens2类代码
package JUnit4Printtokens2;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
public class Printtokens2 {
static int error = 0;
static int keyword = 1;
static int spec_symbol = 2;
static int identifier = 3;
static int num_constant = 41;
static int str_constant = 42;
static int char_constant = 43;
static int comment = 5;
/***********************************************/
/* NMAE: open_character_stream */
/* INPUT: a filename */
/* OUTPUT: a BufferedReader */
/* DESCRIPTION: when not given a filename, */
/* open stdin,otherwise open */
/* the existed file */
/***********************************************/
BufferedReader open_character_stream(String fname) {
BufferedReader br = null;
if (fname == null) {
br = new BufferedReader(new InputStreamReader(System.in));
} else {
try {
FileReader fr = new FileReader(fname);
br = new BufferedReader(fr);
} catch (FileNotFoundException e) {
System.out.print("The file " + fname +" doesn't exists\n");
e.printStackTrace();
}
}
return null;
}
/**********************************************/
/* NAME: get_char */
/* INPUT: a BufferedReader */
/* OUTPUT: a character */
/**********************************************/
int get_char(BufferedReader br){
int ch = 0;
try {
br.mark(3);
ch= br.read();
} catch (IOException e) {
e.printStackTrace();
}
return ch;
}
/***************************************************/
/* NAME: unget_char */
/* INPUT: a BufferedReader,a character */
/* OUTPUT: a character */
/* DESCRIPTION: move backward */
/***************************************************/
char unget_char (int ch,BufferedReader br) {
try {
br.reset();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
/********************************************************/
/* NAME: open_token_stream */
/* INPUT: a filename */
/* OUTPUT: a BufferedReader */
/* DESCRIPTION: when filename is EMPTY,choice standard */
/* input device as input source */
/********************************************************/
BufferedReader open_token_stream(String fname)
{
BufferedReader br;
if(fname.equals(null))
br=open_character_stream(null);
else
br=open_character_stream(fname);
return br;
}
/********************************************************/
/* NAME : get_token */
/* INPUT: a BufferedReader */
/* OUTPUT: a token string */
/* DESCRIPTION: according the syntax of tokens,dealing */
/* with different case and get one token */
/********************************************************/
String get_token(BufferedReader br)
{
int i=0,j;
int id=0;
int res = 0;
char ch = '\\0';
StringBuilder sb = new StringBuilder();
try {
res = get_char(br);
if (res == -1) {
return null;
}
ch = (char)res;
while(ch=='\t'||ch=='\n' || ch == '\r') /* strip all blanks until meet characters */
{
res = get_char(br);
ch = (char)res;
}
if(res == -1)return null;
sb.append(ch);
if(is_spec_symbol(ch)==true)return sb.toString();
if(ch =='"')id=2; /* prepare for string */
if(ch ==59)id=1; /* prepare for comment */
res = get_char(br);
if (res == -1) {
unget_char(ch,br);
return sb.toString();
}
ch = (char)res;
while (is_token_end(id,res) == false)/* until meet the end character */
{
sb.append(ch);
br.mark(4);
res = get_char(br);
if (res == -1) {
break;
}
ch = (char)res;
}
if(res == -1) /* if end character is eof token */
{
unget_char(ch,br); /* then put back eof on token_stream */
return sb.toString();
}
if(is_spec_symbol(ch)==true) /* if end character is special_symbol */
{
unget_char(ch,br); /* then put back this character */
return sb.toString();
}
if(id==1) /* if end character is " and is string */
{
sb.append(ch);
return sb.toString();
}
if(id==0 && ch==59)
/* when not in string or comment,meet ; */
{
unget_char(ch,br); /* then put back this character */
return sb.toString();
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString(); /* return nomal case token */
}
/*******************************************************/
/* NAME: is_token_end */
/* INPUT: a character,a token status */
/* OUTPUT: a BOOLEAN value */
/*******************************************************/
static boolean is_token_end(int str_com_id, int res)
{
if(res==-1)return(true); /* is eof token? */
char ch = (char)res;
if(str_com_id==1) /* is string token */
{
if(ch=='"' | ch=='\n' || ch == '\r') /* for string until meet another " */
return true;
else
return false;
}
if(str_com_id==2) /* is comment token */
{
if(ch=='\n' || ch == '\r' || ch=='\t') /* for comment until meet end of line */
return true;
else
return false;
}
if(is_spec_symbol(ch)==true) return true; /* is special_symbol? */
if(ch ==' ' || ch=='\n'|| ch=='\r' || ch==59) return true;
return false; /* other case,return FALSE */
}
/****************************************************/
/* NAME : token_type */
/* INPUT: a token */
/* OUTPUT: an integer value */
/* DESCRIPTION: the integer value is corresponding */
/* to the different token type */
/****************************************************/
static int token_type(String tok)
{
if(is_keyword(tok))return(keyword);
if(is_spec_symbol(tok.charAt(0)))return(spec_symbol);
if(is_identifier(tok))return(identifier);
if(is_num_constant(tok))return(num_constant);
if(is_str_constant(tok))return(str_constant);
if(is_char_constant(tok))return(char_constant);
if(is_comment(tok))return(comment);
return(error); /* else look as error token */
}
/****************************************************/
/* NAME: print_token */
/* INPUT: a token */
/****************************************************/
void print_token(String tok)
{ int type;
type=token_type(tok);
if(type==error)
{
System.out.print("error,\"" + tok + "\".\n");
}
if(type==keyword)
{
System.out.print("keyword,\"" + tok + "\".\n");
}
if(type==spec_symbol)print_spec_symbol(tok);
if(type==identifier)
{
System.out.print("identifier,\"" + tok + "\".\n");
}
if(type==num_constant)
{
System.out.print("numeric," + tok + ".\n");
}
if(type==char_constant)
{
System.out.print("character,\"" + tok.charAt(1) + "\".\n");
}
}
/* the code for tokens judgment function */
/*************************************/
/* NAME: is_comment */
/* INPUT: a token */
/* OUTPUT: a BOOLEAN value */
/*************************************/
static boolean is_comment(String ident)
{
if( ident.charAt(0) ==59 ) /* the char is 59 */
return true;
else
return false;
}
/*************************************/
/* NAME: is_keyword */
/* INPUT: a token */
/* OUTPUT: a BOOLEAN value */
/*************************************/
static boolean is_keyword(String str)
{
if (str.equals("and") || str.equals("or") || str.equals("if") ||
str.equals("xor")||str.equals("lambda")||str.equals("=>"))
return true;
else
return false;
}
/*************************************/
/* NAME: is_char_constant */
/* INPUT: a token */
/* OUTPUT: a BOOLEAN value */
/*************************************/
static boolean is_char_constant(String str)
{
if (str.length() > 2 && str.charAt(0)=='#' && Character.isLetter(str.charAt(1)))
return true;
else
return false;
}
/*************************************/
/* NAME: is_num_constant */
/* INPUT: a token */
/* OUTPUT: a BOOLEAN value */
/*************************************/
static boolean is_num_constant(String str)
{
int i=1;
if ( Character.isDigit(str.charAt(0)))
{
while ( i <= str.length() && str.charAt(i) != '\\0' ) /* until meet token end sign */
{
if(Character.isDigit(str.charAt(i+1)))\t
i++;
else
return false;
}
return true;
}
else
return false; /* other return FALSE */
}
/*************************************/
/* NAME: is_str_constant */
/* INPUT: a token */
/* OUTPUT: a BOOLEAN value */
/*************************************/
static boolean is_str_constant(String str)
{
int i=1;
if ( str.charAt(0) =='"')
{
while (i < str.length() && str.charAt(0)!='\\0') /* until meet the token end sign */
{
if(str.charAt(i)=='"')
return true; /* meet the second " */
else
i++;
}
return true;
}
else
return false; /* other return FALSE */
}
/*************************************/
/* NAME: is_identifier */
/* INPUT: a token */
/* OUTPUT: a BOOLEAN value */
/*************************************/
static boolean is_identifier(String str)
{
int i=0;
if ( Character.isLetter(str.charAt(0)) )
{
while(i < str.length() && str.charAt(i) !='\\0' ) /* unti meet the end token sign */
{
if(Character.isLetter(str.charAt(i)) || Character.isDigit(str.charAt(i)))
i++;
else
return false;
}
return false;
}
else
return true;
}
/******************************************/
/* NAME: unget_error */
/* INPUT: a BufferedReader */
/* OUTPUT: print error message */
/******************************************/
static void unget_error(BufferedReader br)
{
System.out.print("It can not get charcter\n");
}
/*************************************************/
/* NAME: print_spec_symbol */
/* INPUT: a spec_symbol token */
/* OUTPUT : print out the spec_symbol token */
/* according to the form required */
/*************************************************/
static void print_spec_symbol(String str)
{
if (str.equals("{"))
{
System.out.print("lparen.\n");
return;
}
if (str.equals(")"))
{
System.out.print("rparen.\n");
return;
}
if (str.equals("["))
{
System.out.print("lsquare.\n");
return;
}
if (str.equals("]"))
{
System.out.print("rsquare.\n");
return;
}
if (str.equals("'"))
{
System.out.print("quote.\n");
return;
}
if (str.equals("`"))
{
System.out.print("bquote.\n");
return;
}
}
/*************************************/
/* NAME: is_spec_symbol */
/* INPUT: a token */
/* OUTPUT: a BOOLEAN value */
/*************************************/
static boolean is_spec_symbol(char c)
{
if (c == '(')
{
return true;
}
if (c == ')')
{
return true;
}
if (c == '[')
{
return true;
}
if (c == ']')
{
return true;
}
if (c == '/')
{
return true;
}
if (c == '`')
{
return true;
}
if (c == ',')
{
return true;
}
return false; /* others return FALSE */
}
public static void main(String[] args) throws IOException {
String fname = null;
if (args.length == 0) { /* if not given filename,take as '' */
fname = new String();
} else if (args.length == 1) {
fname = args[1];
} else {
System.out.print("Error!,please give the token stream\n");
System.exit(0);
}
Printtokens2 t = new Printtokens2();
BufferedReader br = t.open_token_stream(fname); /* open token stream */
String tok = t.get_token(br);
while (tok != null) { /* take one token each time until eof */
t.print_token(tok);
tok = t.get_token(br);
}
System.exit(0);
}
}
原文地址: https://www.cveoy.top/t/topic/nnrQ 著作权归作者所有。请勿转载和采集!