1.在线OJ项目单元测试
1.1 单元测试的介绍
- 测试阶段: 编码后
- 测试对象:最小模块
- 测试依据:代码+注释+详细设计文档
- 测试方法:白盒测试
- 测试内容:按照单元设计的测试文档参数,输出/边界测试,路径测试
1.2测试执行
- 首先在pom.xml文件中插入依赖
- 在file-settings-pluguins搜素Junit并安装
- 开始做单元测试
- 对要做的某一个类的方法进行测试,先选择要测试的这个类,然后点击 ctrl+shift+t,然后选择要测试的方法:
- 选择要测试的方法之后点击确定,然后会对类里面的各个方法进行测试,然后进行观察并且运行:
package problem;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.*;
public class ProblemDAOTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@org.junit.Ignore
@Test
public void insert() {
Problem problem = new Problem();
problem.setId(9);
problem.setTitle("两数之和");
problem.setLevel("简单");
problem.setDescription("给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。\n" +
"\n" +
"你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。\n" +
"\n" +
"你可以按任意顺序返回答案。\n" +
"\n" +
" \n" +
"\n" +
"示例 1:\n" +
"\n" +
"输入:nums = [2,7,11,15], target = 9 u d eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\n" +
"输出:[0,1]\n" +
"解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。\n" +
"示例 2:\n" +
"\n" +
"输入:nums = [3,2,4], target = 6\n" +
"输出:[1,2]\n" +
"示例 3:\n" +
"\n" +
"输入:nums = [3,3], target = 6\n" +
"输出:[0,1]\n" +
"\n" +
"来源:力扣(LeetCode)\n" +
"链接:https://leetcode-cn.com/problems/two-sum\n" +
"著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。");
problem.setTemplateCode("class Solution {\n" +
" public int[] twoSum(int[] nums, int target) {\n" +
"\n" +
" }\n" +
"}");
problem.setTestCode("public static void main(String[] args) {\n" +
" Solution solution = new Solution();\n" +
" int [] arr ={2,7,11,15};\n" +
" int target =9;\n" +
" int [] result = solution.twoSum(arr,9);\n" +
" if(result.length == 2 && result[0] == 0 && result[1] == 1){\n" +
" System.out.println(\"TestCase Ok!\");\n" +
" }else{\n" +
" System.out.println(\"TestCase Failed! arr:{2,7,11,15},target:9\");\n" +
" }\n" +
"\n" +
" int [] arr2 = {3,2,4};\n" +
" int target2 = 6;\n" +
" int [] result2 = solution.twoSum(arr2,target2);\n" +
" if(result2.length == 2 && result2[0] == 1 && result2[1] == 2){\n" +
" System.out.println(\"TestCase Ok!\");\n" +
" }else{\n" +
" System.out.println(\"TestCase Failed! arr:{3,2,4},target:6\");\n" +
" }\n" +
" }");
ProblemDAO problemDAO = new ProblemDAO();
problemDAO.insert(problem);
}
@Ignore
@Test
public void delete() {
int problemId = 1;
ProblemDAO problemDAO = new ProblemDAO();
problemDAO.delete(problemId);
}
@Test
public void selectAll() {
ProblemDAO problemDAO = new ProblemDAO();
List<Problem> problems = problemDAO.selectAll();
System.out.println(problems);
}
@Test
public void selectOne() {
ProblemDAO problemDAO = new ProblemDAO();
Problem problem = ProblemDAO.selectOne(4);
System.out.println(problem);
}
}
- 在这里的单元测试主要是针对数据库进行增删改查,除了看运行后的打印结果,应该看刷新界面是否也会有相应的改变。
|