• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    追求代碼質量: 可重復的系統測試

    發布: 2009-6-22 10:41 | 作者: 不詳 | 來源: 領測軟件測試網采編 | 查看: 45次 | 進入軟件測試論壇討論

    領測軟件測試網 在測試加入到 servlet 容器的 Web 應用程序時,編寫符合邏輯的可重復的測試尤其需要技巧。在 Andrew Glover 的提高代碼質量的這個續篇中,他介紹了 Cargo,這是一個以通用方式自動化容器管理的開源框架,有了這個框架,您可以隨時編寫符合邏輯的可重復的系統測試。
    在本質上,像 JUnit 和 TestNG 一樣的測試框架方便了可重復性測試的創建。由于這些框架利用了簡單 Boolean 邏輯(以 assert 方法的形式)的可靠性,這使得無人為干預而運行測試成為可能。事實上,自動化是測試框架的主要優點之一 —— 我能夠編寫一個用于斷言具體行為的相當復雜的測試,且一旦這些行為有所改變,框架就會報告一個人人都能明白的錯誤。

        利用成熟的測試框架會帶來框架 可重復性的優點,這是顯而易見的。但邏輯的 可重復性卻取決于您。例如,考慮創建用于驗證 Web 應用程序的可重復測試的情況,一些 JUnit 擴展框架(如 JWebUnit 和 HttpUnit)在協助自動化的 Web 測試方面非常好用。但是,使測試的 plumbing 可重復則是開發人員的任務,而這在部署 Web 應用程序資源時很難進行。

    實際的 JWebUnit 測試的構造過程相當簡單,如清單 1 所示:

    清單 1. 一個簡單的 JWebUnit 測試

    package test.come.acme.widget.Web;

    import net.sourceforge.jwebunit.WebTester;
    import junit.framework.TestCase;

    public class WidgetCreationTest extends TestCase {
    private WebTester tester;

    protected void setUp() throws Exception {
      this.tester = new WebTester();
      this.tester.getTestContext().
       setBaseUrl("http://localhost:8080/widget/");       
    }

    public void testWidgetCreation() {
      this.tester.beginAt("/CreateWidget.html");               
      this.tester.setFormElement("widget-id", "893-44");
      this.tester.setFormElement("part-num", "rt45-3");

      this.tester.submit();               
      this.tester.assertTextPresent("893-44");
      this.tester.assertTextPresent("successfully created.");
    }
    }  

        這個測試與一個 Web 應用程序通信,并試圖創建一個基于該交互的小部件。該測試隨后校驗此部件是否被成功創建。讀過本系列之前部分的讀者們也許會注意到該測試的一個微妙的可重復性問題。您注意到了嗎?如果這個測試用例連續 運行兩次會怎樣呢?

        改進代碼質量

        不要錯過了 Andrew 的附隨的 討論論壇,獲取有關代碼編寫方法、測試框架及編寫高質量代碼的幫助。

        由這個小部件實例(即,widget-id)的驗證方面可以判斷出,可以安全地做出這樣的假設,即此應用程序中的數據庫約束很可能會阻止創建一個已經存在的額外的小部件。由于缺少了一個在運行另一個測試前刪除此測試用例的目標小部件的過程,如果再連續運行兩次,這個測試用例非常有可能會失敗。

        幸運的是,如前面文章中所探討的那樣,有一個有助于數據庫-依賴性(database-dependent)測試用例可重復性的機制 —— 即 DbUnit。

        使用 DbUnit

        改進 清單 1 中的測試用例來使用 DbUnit 是非常簡單的。DbUnit 只需要一些插入數據庫的數據和一個相應的數據庫連接,如清單 2 所示:


    清單 2. 用 DbUnit 進行的數據庫-依賴性測試

    package test.come.acme.widget.Web;

    import java.io.File;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    import org.dbunit.database.DatabaseConnection;
    import org.dbunit.database.IDatabaseConnection;
    import org.dbunit.dataset.DataSetException;
    import org.dbunit.dataset.IDataSet;
    import org.dbunit.dataset.xml.FlatXmlDataSet;
    import org.dbunit.operation.DatabaseOperation;

    import net.sourceforge.jwebunit.WebTester;
    import junit.framework.TestCase;

    public class RepeatableWidgetCreationTest extends TestCase {
    private WebTester tester;

    protected void setUp() throws Exception {
      this.handleSetUpOperation();
      this.tester = new WebTester();
      this.tester.getTestContext().
       setBaseUrl("http://localhost:8080/widget/");       
    }

    public void testWidgetCreation() {
      this.tester.beginAt("/CreateWord.html");               
      this.tester.setFormElement("widget-id", "893-44");
      this.tester.setFormElement("part-num", "rt45-3");

      this.tester.submit();               
      this.tester.assertTextPresent("893-44");
      this.tester.assertTextPresent("successfully created.");
    }

    private void handleSetUpOperation() throws Exception{
      final IDatabaseConnection conn = this.getConnection();
      final IDataSet data = this.getDataSet();        
      try{
       DatabaseOperation.CLEAN_INSERT.execute(conn, data);
      }finally{
       conn.close();
      }
    }

    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/

    TAG: 代碼 系統 質量

    41/41234>

    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備2023014753號-2
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>