1. 程式人生 > >軟件測試 junit

軟件測試 junit

class exception 解析 rsa tput 條件組合覆蓋 inf pla equal

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