全国大学生软件测试开发者测试大赛笔记总结
(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)抛出异常
@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)浮点数
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
(5)关于转义字符
(6)关于集合
HashMap< String, Integer> hashMap = new HashMap < String, Integer> ( ) ;
List< ? > l = new ArrayList < > ( ) ;
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 ( )
如果仍有元素可以迭代,则返回 true 。next ( )
(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) ;
(10)测试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 ) ;
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测试
public static int readValue ( )
{
Scanner in = new Scanner ( System. in) ;
String tmpInt = in. nextLine ( ) ;
if ( isNumeric ( tmpInt) )
return Integer. parseInt ( tmpInt) ;
else
return 0 ;
}
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)输出测试
public class WelcomePageTest {
private PrintStream console = null;
private ByteArrayOutputStream bytes = null;
@Before
public void setUp ( ) {
bytes = new ByteArrayOutputStream ( ) ;
console = System. out;
System. setOut ( new PrintStream ( 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" , "" ) ) ;
}
}
(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) ) ;
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: [ ] { } >
少了空格