软件测试--开发者测试例子
最近几天学习软件测试,在软件的测试上,用eclipse和Junit,使用了EclEmma java Code Coverage的测是软件。
从help进入,进行安装
eclipse环境就不用说了,先安装完Java -> 然后eclipse解压,这里就不多说了。
说一下软件测试大赛,进入官网http://www.mooctest.org/#/,然后点击右侧的报名,账号注册,报名就OK了
也就是这个网站 http://www.mooctest.net/ 可以之接点击进入,进入选择报名项目。然后等着考试就行了。
我要是在水,估计会被人骂死了。
这里就上点干货,
例子:开发者测试全局练习1
这里面有两个题目:1.Triangle:
我做这个题目没有得到多少分数,高手别喷,
package net.mooctest;
public class Triangle {
protected long lborderA = 0;
public class Triangle {
protected long lborderA = 0;
protected long lborderB = 0;
protected long lborderC = 0;
// Constructor
public Triangle(long lborderA, long lborderB, long lborderC) {
public Triangle(long lborderA, long lborderB, long lborderC) {
this.lborderA = lborderA; // 2
this.lborderB = lborderB; // 3
this.lborderC = lborderC; // 4
}
/**
* check if it is a triangle // 检测是不是三角形
*
* @return true for triangle and false not // 1/0
*/
public boolean isTriangle(Triangle triangle) {
boolean isTriangle = false;
* check if it is a triangle // 检测是不是三角形
*
* @return true for triangle and false not // 1/0
*/
public boolean isTriangle(Triangle triangle) {
boolean isTriangle = false;
// check boundary
if ((triangle.lborderA > 0 && triangle.lborderA <= Long.MAX_VALUE)
&& (triangle.lborderB > 0 && triangle.lborderB <= Long.MAX_VALUE)
&& (triangle.lborderC > 0 && triangle.lborderC <= Long.MAX_VALUE)) {
if ((triangle.lborderA > 0 && triangle.lborderA <= Long.MAX_VALUE)
&& (triangle.lborderB > 0 && triangle.lborderB <= Long.MAX_VALUE)
&& (triangle.lborderC > 0 && triangle.lborderC <= Long.MAX_VALUE)) {
// check if subtraction of two border larger than the third
if (diffOfBorders(triangle.lborderA, triangle.lborderB) < triangle.lborderC
&& diffOfBorders(triangle.lborderB, triangle.lborderC) < triangle.lborderA
&& diffOfBorders(triangle.lborderC, triangle.lborderA) < triangle.lborderB) {
isTriangle = true;
}
if (diffOfBorders(triangle.lborderA, triangle.lborderB) < triangle.lborderC
&& diffOfBorders(triangle.lborderB, triangle.lborderC) < triangle.lborderA
&& diffOfBorders(triangle.lborderC, triangle.lborderA) < triangle.lborderB) {
isTriangle = true;
}
}
return isTriangle;
}
return isTriangle;
}
/**
* Check the type of triangle
*
* Consists of "Illegal", "Regular", "Scalene", "Isosceles"
*/
public String getType(Triangle triangle) {
String strType = "Illegal";
* Check the type of triangle
*
* Consists of "Illegal", "Regular", "Scalene", "Isosceles"
*/
public String getType(Triangle triangle) {
String strType = "Illegal";
if (isTriangle(triangle)) { // 检测是不是三角形
// Is Regular
if (triangle.lborderA == triangle.lborderB
&& triangle.lborderB == triangle.lborderC) {
strType = "Regular";
} // 等边三角形
// If scalene
else if ((triangle.lborderA != triangle.lborderB)
&& (triangle.lborderB != triangle.lborderC)
&& (triangle.lborderA != triangle.lborderC)) {
strType = "Scalene";
} // 不等边三角形
// if isosceles
else {
strType = "Isosceles";
} // 等腰三角形
}
// Is Regular
if (triangle.lborderA == triangle.lborderB
&& triangle.lborderB == triangle.lborderC) {
strType = "Regular";
} // 等边三角形
// If scalene
else if ((triangle.lborderA != triangle.lborderB)
&& (triangle.lborderB != triangle.lborderC)
&& (triangle.lborderA != triangle.lborderC)) {
strType = "Scalene";
} // 不等边三角形
// if isosceles
else {
strType = "Isosceles";
} // 等腰三角形
}
return strType;
}
}
/**
* calculate the diff between borders
*
* */
public long diffOfBorders(long a, long b) {
return (a > b) ? (a - b) : (b - a);
}
* calculate the diff between borders
*
* */
public long diffOfBorders(long a, long b) {
return (a > b) ? (a - b) : (b - a);
}
/**
* get length of borders
*/
public long[] getBorders() {
long[] borders = new long[3];
borders[0] = this.lborderA;
borders[1] = this.lborderB;
borders[2] = this.lborderC;
return borders;
}
}
* get length of borders
*/
public long[] getBorders() {
long[] borders = new long[3];
borders[0] = this.lborderA;
borders[1] = this.lborderB;
borders[2] = this.lborderC;
return borders;
}
}
这是被测试的代码,有部分注释是本人加上了,理论上什么都不要加,但是为了增加测试的速度,我就自己加上了,在比赛中可以加上,只要不改变源码就OK。这个说的是一个三角形的例子,我就说一下这个例子,
1.要做到代码覆盖
2.在做到全覆盖了,或者无法进行下去的时候,进行第二步,进行if等判断的检查,重点检查 || &&这些的存在更加注意,在if选择里,这种覆盖很容易忽略,其次就是分支的覆盖,if i怎样然后就没有else了,这时候既要进行i > 0的操作,又要进行 i < 0的操作。这中情况会出现分支覆盖的遗漏。
3.这时候基本就在70-80甚至更高的分数了。这时候如果有时间,那么我们就进行,变异测试,变异测试这个是(裁判)在(被测)代码上进行修改,让程序出现问题。这个就比较看运气了,所以只要前面的得到足够的分数,省赛是小事情。在变异测试上,就是多想,裁判会在哪里进行修改,当然代码可以大量的添加,用量的积累进行质的变化。
package net.mooctest;
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.Arrays;
/*
* 68.75 95.41
* 71.88 95.41
* 81.25 95.41
*
*/
public class TriangleTest {
Triangle T1 = new Triangle(2, 3, 4); // 是三角形 // 普通三角形
Triangle T2 = new Triangle(1, 1, 1); // 是三角形 // 等边三角形
Triangle T3 = new Triangle(2, 2, 3); // 是三角形 // 等腰三角形
Triangle T4 = new Triangle(1, 1, 3); // 不是三角形 // 非三角形
Triangle T5 = new Triangle(5, 2, 2); // 不是三角形 // 非三角形
Triangle T6 = new Triangle(2, 5, 2); // 不是三角形 // 非三角形
Triangle T7 = new Triangle(3, 2, 1); // 不是三角形 // 非三角形
Triangle T8 = new Triangle(-1, 2, 1); // 不是三角形 // 非三角形
Triangle T9 = new Triangle(1, -1, 1); // 不是三角形 // 非三角形
Triangle T10 = new Triangle(3, 2, -1); // 不是三角形 // 非三角形
long[] Border = {2,3,4};
@Test
public void testIsTriangle() {
assertEquals(false, T8.isTriangle(T8));
assertEquals(false, T9.isTriangle(T9));
assertEquals(false, T10.isTriangle(T10));
assertEquals("Scalene", T1.getType(T1));
assertEquals("Regular", T2.getType(T2));
assertEquals("Isosceles", T3.getType(T3));
assertEquals("Illegal", T4.getType(T4));
assertEquals("Illegal", T5.getType(T5));
assertEquals("Illegal", T6.getType(T6));
assertEquals("Illegal", T7.getType(T7));
assertEquals("Illegal", T8.getType(T8));
assertEquals("Illegal", T9.getType(T9));
assertEquals("Illegal", T10.getType(T10));
assertEquals(1, T1.diffOfBorders(T1.lborderA,T1.lborderB));
assertEquals(1, T1.diffOfBorders(T1.lborderC,T1.lborderB));
assertEquals(2, T4.diffOfBorders(T4.lborderB,T4.lborderC));
assertEquals(true, Arrays.equals(T1.getBorders(), Border));
//assertEquals("Regular",T1.getType(T1));
//assertEquals("Scalene",T1.getType(T1));
//assertEquals("Isosceles",T1.getType(T1));
//assertEquals(, T1.getBorders());
}
}
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.Arrays;
/*
* 68.75 95.41
* 71.88 95.41
* 81.25 95.41
*
*/
public class TriangleTest {
Triangle T1 = new Triangle(2, 3, 4); // 是三角形 // 普通三角形
Triangle T2 = new Triangle(1, 1, 1); // 是三角形 // 等边三角形
Triangle T3 = new Triangle(2, 2, 3); // 是三角形 // 等腰三角形
Triangle T4 = new Triangle(1, 1, 3); // 不是三角形 // 非三角形
Triangle T5 = new Triangle(5, 2, 2); // 不是三角形 // 非三角形
Triangle T6 = new Triangle(2, 5, 2); // 不是三角形 // 非三角形
Triangle T7 = new Triangle(3, 2, 1); // 不是三角形 // 非三角形
Triangle T8 = new Triangle(-1, 2, 1); // 不是三角形 // 非三角形
Triangle T9 = new Triangle(1, -1, 1); // 不是三角形 // 非三角形
Triangle T10 = new Triangle(3, 2, -1); // 不是三角形 // 非三角形
long[] Border = {2,3,4};
@Test
public void testIsTriangle() {
assertEquals(false, T8.isTriangle(T8));
assertEquals(false, T9.isTriangle(T9));
assertEquals(false, T10.isTriangle(T10));
assertEquals("Scalene", T1.getType(T1));
assertEquals("Regular", T2.getType(T2));
assertEquals("Isosceles", T3.getType(T3));
assertEquals("Illegal", T4.getType(T4));
assertEquals("Illegal", T5.getType(T5));
assertEquals("Illegal", T6.getType(T6));
assertEquals("Illegal", T7.getType(T7));
assertEquals("Illegal", T8.getType(T8));
assertEquals("Illegal", T9.getType(T9));
assertEquals("Illegal", T10.getType(T10));
assertEquals(1, T1.diffOfBorders(T1.lborderA,T1.lborderB));
assertEquals(1, T1.diffOfBorders(T1.lborderC,T1.lborderB));
assertEquals(2, T4.diffOfBorders(T4.lborderB,T4.lborderC));
assertEquals(true, Arrays.equals(T1.getBorders(), Border));
//assertEquals("Regular",T1.getType(T1));
//assertEquals("Scalene",T1.getType(T1));
//assertEquals("Isosceles",T1.getType(T1));
//assertEquals(, T1.getBorders());
}
}
这里就不要脸的贴上我的测试代码,分数不高,
只有81.25和95.41