使用HTTPUnit进行功能测试

  对每个Web程序都需要注意的登陆验证进行测试。

  测试目标:验证用户登陆是否成功

  测试过程:

  1、输入登陆地址的页面地址,验证该页面是否可被正常访问。

  2、验证被访问的页面是否是登陆页面。

  3、输入非法用户名、密码,验证登陆失败。

  4、输入合法用户名、密码,验证登陆成功。首先写一个测试接口,起名为LoginTestInfo:

publicinterface LoginTestInfo{
public void testValidPage() throws Exception;
public void testIsLoginPage() throws Exception;
public void testBadLogin() throws Exception;
public void testGoodLogin() throws Exception;
}


  实现一个Junit TestCase 同时 implements LoginTestInfo 接口:

import java.net.URL;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner; import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.GetMethodWebRequest;
 
public class LoginTest extends TestCase implements LoginTestInfo{
      private WebConversation browser;
      private WebRequest request;
      private WebResponse response;
      private String url = "http://localhost:8080/index.html";
      public void setUp() throws Exception{
         browser = new WebConversation();
         request = new GetMethodWebRequest(url);
         response = browser.getResponse(request);
 
      }
      //输入登陆地址的页面地址,验证该页面是否可被正常访问
      public void testValidPage() throws Exception{
         assertNotNull("localhost在网络上不存在!",response);
      }
 
      //验证被访问的页面是否是登陆页面
 
      public void testIsLoginPage() throws Exception{
         URL currentUrl = response.getURL();
         String currentUrlStr = currentUrl.getProtocol() + "://" +currentUrl.getHost() + currentUrl.getPath();
         assertEquals("登陆页面不是localhost首页!" ,currentUrlStr,url);
      }
 
      //输入非法用户名、密码,验证登陆失败
      public void testBadLogin() throws Exception{
         WebForm form = response.getForms()[0];
         form.setParameter("userName","baduser");
         form.setParameter("passWord","bad");
         request = form.getRequest();
         response = browser.getResponse(request);
         assertTrue("您的用户名和密码在localhost没有备案!",response.getText().indexOf("localhost") != -1);
      }
 
      //输入合法用户名、密码,验证登陆成功
      public void testGoodLogin() throws Exception{
 
        WebForm form = response.getForms()[0];
        form.setParameter("userName","smile_xunn");
        form.setParameter("passWord","*********");//此处需要填写真实密码
        request = form.getRequest();
        response = browser.getResponse(request);
        assertTrue("转到‘localhost‘网络失败!",response.getText().indexOf("localhost") != -1);
      }
 
      public static TestSuite suite(){
        return new TestSuite(LoginTest.class);
      }
 
      public static void main(String args[]){
        TestRunner.run(suite());
      }
 
}


  如果你的代码质量很高,这个测试能够通过,出现如下信息:….

  Time: 7.203

  OK (4 tests)

  JMeter的性能测试

  上面是JMeter的界面的一部分,限于篇幅,具体的使用可以参考Apache网站。

  JUnitPerf的负载测试

  在此测试中,可以整合前述的所有方法,把所有放在一块儿进行。JUnitPerf与现有的JUnit测试共同工作。如果你对特定代码有性能要求,那么创建JUnitPerf测试来测试你的代码符合标准,并且确保在你重构代码后代码仍然符合标准。

  三、结语

  通过以上测试,我想在你的产品交付用户时,一定可以拍着胸脯说:我们是好的,这一切都被证明了。无论是项目的扩展还是功能的改动,有这些工具的帮助,都不将是可怕的越滚越大的雪球。

  作为技术至上理论的忠实拥护者,我认为一个测试框架的成功与否都要在用户的使用后评定。理论要靠实践来证实,上述测试正好对我们所学的软件工程课程以及软件实践课程提供了技术说明。作为开源项目,也许上述测试框架还存在着某些弊病,相信在开源精神的鼓舞下,一定会更加尽善尽美。