k41d3n
asked on
junit, httpunit and logging
Alright, here is what I am trying to do.
I am using httpunit to test a website.
I also have a database to record test results.
I am very new to unit testing.
When I am running a test, how do I get junit to record a failure, or even better, log a failure to my database? Here is one of my tests:
/*
* Created on May 5, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.apc.qa.ate.test.http;
import java.io.IOException;
import java.net.MalformedURLExcep tion;
import org.apache.log4j.Logger;
import org.xml.sax.SAXException;
import com.apc.qa.ate.functions.H ttpFunctio ns;
import com.apc.qa.ate.test.LoginP asswordTes tCase;
import com.apc.qa.ate.util.FieldC onstants;
import com.apc.qa.ate.util.LinkCo nstants;
import com.apc.qa.ate.util.Util;
import com.apc.qa.ate.util.ValueC onstants;
import com.meterware.httpunit.Web Conversati on;
import com.meterware.httpunit.Web Form;
import com.meterware.httpunit.Web Image;
import com.meterware.httpunit.Web Link;
import com.meterware.httpunit.Web Response;
/**
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*
* @author marcadam
**/
public class DeviceIdentification extends LoginPasswordTestCase {
public DeviceIdentification(Strin g name, String _uname, String _pword) {
super(name, _uname, _pword);
}
/**
* Logger for this class
*/
private static final Logger atelog = Logger.getLogger(HttpFunct ions.class );
public void testDeviceInfo()throws Exception {
try {
//New Web Conversation
WebConversation wc = new WebConversation();
//Web Response
WebResponse resp = wc.getResponse(Util.getLog inUrl());
//Get the Login Form
WebForm form = resp.getForms()[0];
//Set the form username and password
form.setParameter("login_u sername", uname);
form.setParameter("login_p assword", pword);
//Submit the form
form.submit();
//Now check to see if you can access the main URL
resp = wc.getResponse(Util.getMai nIndex());
//Assert that the login worked
assertTrue("Login not accepted",
resp.getText().indexOf("Up dated:") != -1);
//Log the login only if you are logged in
atelog.info("Logged in properly");
WebLink admin = resp.getLinkWith(LinkConst ants.LINK_ ADMIN);
assertNotNull("Finding Admin Link", admin);
atelog.debug("Found Admin Link");
resp = admin.click();
WebLink general = resp.getLinkWith(LinkConst ants.LINK_ GEN);
assertNotNull("Finding General Link", general);
atelog.debug("Found General Link");
resp = general.click();
WebLink iden = resp.getLinkWith(LinkConst ants.LINK_ IDEN);
assertNotNull("Finding Identification Link", iden);
atelog.debug("Found Identification Link");
resp = iden.click();
WebForm ident = resp.getForms()[0];
assertNotNull("Finding form", ident);
atelog.debug("Found Web Form");
ident.setParameter(FieldCo nstants.FI ELD_SYSIDE N, ValueConstants.VALUE_SYSID EN);
atelog.info("Setting System Name: " + ValueConstants.VALUE_SYSID EN);
ident.setParameter(FieldCo nstants.FI ELD_SYSCON T, ValueConstants.VALUE_SYSCO NT);
atelog.info("Setting System Contact: " + ValueConstants.VALUE_SYSCO NT);
ident.setParameter(FieldCo nstants.FI ELD_SYSLOC , ValueConstants.VALUE_SYSLO C);
atelog.info("Setting System Location: " + ValueConstants.VALUE_SYSLO C);
ident.submit();
resp = wc.getResponse(Util.getMai nIndex());
admin = resp.getLinkWith(LinkConst ants.LINK_ ADMIN);
assertNotNull("Finding Admin Link", admin);
atelog.debug("Found Admin Link");
resp = admin.click();
general = resp.getLinkWith(LinkConst ants.LINK_ GEN);
assertNotNull("Finding General Link", general);
atelog.debug("Found General Link");
resp = general.click();
iden = resp.getLinkWith(LinkConst ants.LINK_ IDEN);
assertNotNull("Finding Identification Link", iden);
atelog.debug("Found Identification Link");
resp = iden.click();
ident = resp.getForms()[0];
assertEquals(ValueConstant s.VALUE_SY SIDEN, ident.getParameterValue(Fi eldConstan ts.FIELD_S YSIDEN));
assertEquals(ValueConstant s.VALUE_SY SCONT, ident.getParameterValue(Fi eldConstan ts.FIELD_S YSCONT));
assertEquals(ValueConstant s.VALUE_SY SLOC, ident.getParameterValue(Fi eldConstan ts.FIELD_S YSLOC));
atelog.info("System Name Set: " + ValueConstants.VALUE_SYSID EN);
atelog.info("System Contact Set: " + ValueConstants.VALUE_SYSCO NT);
atelog.info("System Location Set: " + ValueConstants.VALUE_SYSLO C);
//Find the logout image
WebImage image = resp.getImageWithSource("i mages/exit .gif");
assertNotNull("Find Logout Image", image);
atelog.debug("Found Logout Image");
//Find the logout link associated with the image
WebLink findLogoutLink = image.getLink();
assertNotNull("Find Logout Link", findLogoutLink);
atelog.debug("Found Logout Link");
//click the link
resp = findLogoutLink.click();
assertNotNull("Logout Page", resp);
assertTrue("Did not Log out", resp.getText().indexOf(
"You are now logged off.") != -1);
atelog.info("Successfully logged out");
} catch (MalformedURLException e) {
atelog.error(e);
e.printStackTrace();
} catch (IOException e) {
atelog.error(e);
e.printStackTrace();
} catch (SAXException e) {
atelog.error(e);
e.printStackTrace();
}
}
}
Right now this test passes, but let's say it were to fail, and it has when I coded it. junit just threw an excepton and quit.
If I put 30 tests in a test suite, and one of them fails, I want to log it, and move on to the next test. Make sense?
There has to be a way to mark a test as failed and continue on.
Thanks!
I am using httpunit to test a website.
I also have a database to record test results.
I am very new to unit testing.
When I am running a test, how do I get junit to record a failure, or even better, log a failure to my database? Here is one of my tests:
/*
* Created on May 5, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.apc.qa.ate.test.http;
import java.io.IOException;
import java.net.MalformedURLExcep
import org.apache.log4j.Logger;
import org.xml.sax.SAXException;
import com.apc.qa.ate.functions.H
import com.apc.qa.ate.test.LoginP
import com.apc.qa.ate.util.FieldC
import com.apc.qa.ate.util.LinkCo
import com.apc.qa.ate.util.Util;
import com.apc.qa.ate.util.ValueC
import com.meterware.httpunit.Web
import com.meterware.httpunit.Web
import com.meterware.httpunit.Web
import com.meterware.httpunit.Web
import com.meterware.httpunit.Web
/**
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*
* @author marcadam
**/
public class DeviceIdentification extends LoginPasswordTestCase {
public DeviceIdentification(Strin
super(name, _uname, _pword);
}
/**
* Logger for this class
*/
private static final Logger atelog = Logger.getLogger(HttpFunct
public void testDeviceInfo()throws Exception {
try {
//New Web Conversation
WebConversation wc = new WebConversation();
//Web Response
WebResponse resp = wc.getResponse(Util.getLog
//Get the Login Form
WebForm form = resp.getForms()[0];
//Set the form username and password
form.setParameter("login_u
form.setParameter("login_p
//Submit the form
form.submit();
//Now check to see if you can access the main URL
resp = wc.getResponse(Util.getMai
//Assert that the login worked
assertTrue("Login not accepted",
resp.getText().indexOf("Up
//Log the login only if you are logged in
atelog.info("Logged in properly");
WebLink admin = resp.getLinkWith(LinkConst
assertNotNull("Finding Admin Link", admin);
atelog.debug("Found Admin Link");
resp = admin.click();
WebLink general = resp.getLinkWith(LinkConst
assertNotNull("Finding General Link", general);
atelog.debug("Found General Link");
resp = general.click();
WebLink iden = resp.getLinkWith(LinkConst
assertNotNull("Finding Identification Link", iden);
atelog.debug("Found Identification Link");
resp = iden.click();
WebForm ident = resp.getForms()[0];
assertNotNull("Finding form", ident);
atelog.debug("Found Web Form");
ident.setParameter(FieldCo
atelog.info("Setting System Name: " + ValueConstants.VALUE_SYSID
ident.setParameter(FieldCo
atelog.info("Setting System Contact: " + ValueConstants.VALUE_SYSCO
ident.setParameter(FieldCo
atelog.info("Setting System Location: " + ValueConstants.VALUE_SYSLO
ident.submit();
resp = wc.getResponse(Util.getMai
admin = resp.getLinkWith(LinkConst
assertNotNull("Finding Admin Link", admin);
atelog.debug("Found Admin Link");
resp = admin.click();
general = resp.getLinkWith(LinkConst
assertNotNull("Finding General Link", general);
atelog.debug("Found General Link");
resp = general.click();
iden = resp.getLinkWith(LinkConst
assertNotNull("Finding Identification Link", iden);
atelog.debug("Found Identification Link");
resp = iden.click();
ident = resp.getForms()[0];
assertEquals(ValueConstant
assertEquals(ValueConstant
assertEquals(ValueConstant
atelog.info("System Name Set: " + ValueConstants.VALUE_SYSID
atelog.info("System Contact Set: " + ValueConstants.VALUE_SYSCO
atelog.info("System Location Set: " + ValueConstants.VALUE_SYSLO
//Find the logout image
WebImage image = resp.getImageWithSource("i
assertNotNull("Find Logout Image", image);
atelog.debug("Found Logout Image");
//Find the logout link associated with the image
WebLink findLogoutLink = image.getLink();
assertNotNull("Find Logout Link", findLogoutLink);
atelog.debug("Found Logout Link");
//click the link
resp = findLogoutLink.click();
assertNotNull("Logout Page", resp);
assertTrue("Did not Log out", resp.getText().indexOf(
"You are now logged off.") != -1);
atelog.info("Successfully logged out");
} catch (MalformedURLException e) {
atelog.error(e);
e.printStackTrace();
} catch (IOException e) {
atelog.error(e);
e.printStackTrace();
} catch (SAXException e) {
atelog.error(e);
e.printStackTrace();
}
}
}
Right now this test passes, but let's say it were to fail, and it has when I coded it. junit just threw an excepton and quit.
If I put 30 tests in a test suite, and one of them fails, I want to log it, and move on to the next test. Make sense?
There has to be a way to mark a test as failed and continue on.
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
NP. :-)
ASKER
After mucking with junit some more I discovered all my assumptions in this question were incorrect.
Thank you.