使用 CruiseControl 和 STAF 建立复杂环境下的编译和测试自动化

发表于:2008-4-11 12:56

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:网络转载

分享:

JUnit Test Case 示例

        本章介绍用于测试 Web Service 的 JUnit Test Case 示例,这个例子将详细说明如何利用HttpUnit 来测试 Web Service。在这个例子中,共有三方参与,分别为 Web Service Provider、Web Service Client 和 Test Client。首先由 Test Client 向 Web Service Client 发出 HTTP 测试请求,Web Service Client 在验证用户身份后,生成相应的 SOAP 请求发给 Web Service Provider,

        Web Service Provider 同样在验证用户身份后为其提供 Web Service 服务,并将结果仍以 SOAP的形式返回到 Web Service Client,再由 Web Service Client 以 HTTP 形式返回结果给 Test Client。最后由 Test Client 对返回结果的正确性进行判断并给出测试结果。JUnit Test Case 示例如清单 21 所示。

清单 21. JUnit Test Case 示例

01 public class SampleTest extends TestCase {

02 private String invoke_client(

03 String requestString, String clientURL, String providerURL) {

04 String result = null;

05 String requestURL = clientURL + "?endpoint=" + providerURL

06 + "&requestString=" + requestString;

07 WebConversation wc = new WebConversation();

08 wc.setAuthorization(SampleConst.Username, SampleConst.Password);

09 WebRequest req = new GetMethodWebRequest(requestURL);

10 WebResponse resp;

11 String responseString;

12 try {

13 resp = wc.getResponse(req);

14 responseString = resp.getText();

15 } catch (AuthorizationRequiredException e) {

16 result = "Invalid UserID or Password.";

17 return result;

18 } catch (Exception e) {

19 result = "Exception on HTTP request";

20 return result;

21 }

22 int checkNames = responseString.indexOf("User names strings match");

23 int checkRequest = responseString.indexOf("Request strings match");

24 if ((checkStrings == -1) | (checkNames == -1) | (checkRequest == -1)) {

25 result = "Strings not match";

26 }

27 return result;

28 }

29 public void test_sample_case_1() {

30 String requestString = "sample_case_1";

31 String result = invoke_client(requestString,

32 SampleConst.ClientWebURL,SampleConst.ProviderWebURL);

33 assertTrue(result, result == null);

34 }

35 }

        清单 21 展示了访问 Web Service 的 JUnit Test Case 示例。

        其中,第 2-29 行定义了 invoke_client 函数,通过 WebConversation 对 Web Service 进行访问。第 5-6 行设置了 Web Service Client 的地址,endpoint 为 Web Service Provider 的地址。第 8 行为访问 Web Service 添加了身份认证信息。第 12-21 行访问 Web Service,将结果存入 responseString。如果在访问过程中发生异常,则直接返回错误信息。第 22-28 行对返回结果进行验证,如果发现结果错误,则返回错误信息。否则返回 null 表示访问成功。

        第 30-35 行为一个 Test 的例子,通过调用 invoke_client 对 Web Service 进行测试,并验证返回的错误消息是否为 null。如果为 null 表示测试成功,反之则失败。

结论

        本文使用 CruiseControl、STAF(STAX)、JUnit、Rational Functional Tester 通过一个简化的具体场景展示了自动化测试平台的实现。这个平台不仅可以大大简化测试人员的工作量,减轻测试人员的工作负担,也能保证开发版本的稳定性,使开发人员尽早知道新版本的问题,增强开发人员的信心。

1010/10<12345678910
重磅发布,2022软件测试行业现状调查报告~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计

法律顾问:上海漕溪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2023
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号