gpt4 book ai didi

java - 将 HttpUnit+junit 组织为独立应用程序的最佳设计

转载 作者:行者123 更新时间:2023-12-01 05:47:59 24 4
gpt4 key购买 nike

我的任务是编写一组 http 单元测试,这些测试应该作为单线程应用程序在外部服务器上部署和启动。

经过一些调查和阅读文档后,我得出以下应用程序结构:

public static void main(String... args) throws Exception {

Class[] testCases = {
LoginTest.class,
Test2.class,
Test3.class
};
TestSuite suite = new TestSuite(testCases);

TestResult result = new TestResult();
suite.run(result);

displayResults(result);
}

测试用例看起来像:

public class LoginPageTest extends TestCase {    
public void testLogin() throws IOException, SAXException {

WebConversation wc = new WebConversation();
//Some HttpUnit init code here

loginForm.setParameter("j_username", login);
loginForm.setParameter("j_password", pass);

loginForm.submit();

String expected = String.format("/%s/action/logon.do", endpoint);

assertEquals(wc.getCurrentPage().getURL().getPath(), expected);
}
}

有人做过类似的任务吗?您有一些可以改进此结构的建议吗?如何实现测试用例之间的依赖关系(例如,几乎所有内容都需要用户进行身份验证 ->必须调用 loginTestCase)?

非常感谢任何建议!

提前致谢。

最佳答案

目前,我找到了一种设计测试应用程序的合适方法:

TestCase 类(扩展 jUnit):

public class LogoutTest extends TestCase {

public void testLogout() throws IOException, SAXException {

new Config().initApplication()
.doLogin("user", "pass") // returns WelcomePage class
.goToFilterPage() //Returns another page
.doLogout(); //returns LoginPage class
}
}

一段时间后,我决定为每个页面创建特定的类,该类将具有特定的控制方法,例如“转到下一页”、“注销”、“单击某个按钮”等...

public class WelcomePage extends AbstractPage  {

protected WelcomePage(AbstractPage other) {
super(other);

String expected = String.format("/%s/welcome.do", Config.endpoint);
assertEquals(expected, webConversation.getCurrentPage().getURL().getPath());
logger.info(String.format("Current page: %s", webConversation.getCurrentPage().getURL().getPath() ));
}

public FilterPage goToFilterPage() throws SAXException, IOException {
WebLink link = webConversation.getCurrentPage().getLinkWithID("downloadLink");
assertNotNull("Check if link exist on page", link);
link.click();
return new FilterPage(this);
}
}

AbstractPage 类(实现所有页面的一些通用逻辑):

public abstract class AbstractPage {

protected WebConversation webConversation;

protected Logger logger;

protected AbstractPage(AbstractPage other) {
this.webConversation = other.getWebConversation();
this.logger = Logger.getLogger(this.getClass());
}

protected AbstractPage(WebConversation webConversation) {
this.webConversation = webConversation;
this.logger = Logger.getLogger(this.getClass());
}

public WebConversation getWebConversation() {
return webConversation;
}

public void doLogout() throws SAXException, IOException {
WebLink logoutLink = webConversation.getCurrentPage().getLinkWithID("logoutLink");
assertNotNull(logoutLink);
logoutLink.click();
}
}

关于java - 将 HttpUnit+junit 组织为独立应用程序的最佳设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5460653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com