Hi Hanif,
With JUnit, each test should be independant, and have to run without any order
constraint. Indeed, tests could be run in parallel.
In your case, you have only ONE test. This test is separated in 3 parts (3 Java
functions), but each part depend on the other.
When you write a JUnit test case, JUnit can guess what functions are real tests
and what functions are "helper methods" according to the name.
public void testXXX() is ONE test
public String testXXX()
public void testXXX(String xxx)
private void testXXX()
public void XXXX()
are not test.
In your example, JUnit think you have 3 tests:
testSearchProduct()
testProductAdd()
testOrderAsRegisteredCustomer()
For each test, JUnit executes its runTest internal method, that you override
with :
public void runTest() throws Exception {
setScriptingEnabled(false);
testSearchProduct();
testProductAdd();
testOrderAsRegisteredCustomer();
}
That why your tests run 3 times.
The solution is :
1) don't override runTest
2) rename your 3 helper functions
testSearchProduct() -> searchProduct(String sku)
testProductAdd() -> addProduct(String sku)
testOrderAsRegisteredCustomer() -> orderAsRegisteredCustomer()
3) write only ONE test case :
public void testAddAndOrderProduct() {
searchProduct(sku);
addProduct(sku);
orderAsRegisteredCustomer();
}
Concerning log4j error, you should write your own log4j.xml (or
log4j.properties), and put it in your classpath.
I hope I was "understandable" ;)
++
Julien
----- Message d'origine ----
De : Hanif <[EMAIL PROTECTED]>
À : jwebunit-users <[email protected]>
Envoyé le : Mardi, 6 Février 2007, 16h29mn 58s
Objet : [JWebUnit-users] Test running multiple times and log4j question
Bonjour Julien! First of all, thank you so much for your time and
help. It is very much appreciated!
Julien, with your guidance I managed to write my class and get it to
work on my development environment using intelliJ. In my IDE
environment, I invoke the test run with
junit.swingui.TestRunner.run(testEllenOrdersNew.class); But on my
production environment, which is command line compiling and running, I use:
junit.textui.TestRunner.run(testEllenOrdersNew.class);
When using the ide environment, it runs fine and all tests pass. It
also recognizes my log4j.properties file and doesn't complain about
appenders or lack of initialization.
Now when I port my code over to my production environment (text-based -
non-gui), the compilation runs without errors but runs 3 times in a
row! Moreover, log4j complains about appenders and initialization....
I've included my class, and below, my class path and the output.....
thanks so much Julien!
Hanif
/**
* Created by IntelliJ IDEA.
* User: hanif
* Date: Jan 29, 2007
* Time: 5:07:16 PM
* To change this template use File | Settings | File Templates.
*/
/* if this stops working unexpectedly here are some reasons why:
* 1. the product is hard-coded in and the product may have been
removed from the site
* 2. the exp. year on the visa card has passed.
*
* Some Notes: This class now uses jwebunit 1.4
(jwebunit.sourceforge.net). It comes with its own set of
* dependencies such as junit, log4j, etc. etc...
* Note that a configuration file called log4j.properties
is used by log4j and is sitting somewhere in the
* classpath. This configuration file is useful for
degugging - its configuration can be changed
* on the fly without having to re-compile the class. Its
use for now is commented out (_logger object)
* but it can be used at any time as a good replacement for
system.out.println.
*/
package newline;
import net.sourceforge.jwebunit.junit.WebTestCase;
import net.sourceforge.jwebunit.util.TestingEngineRegistry;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import java.net.URL;
import org.apache.log4j.*;
public class testEllenOrdersNew extends WebTestCase {
private static String homepage =
"http://ellen.hanif.newline.projects.novator.com/";;
private static String sku = "ELCFWATCH";
private static String sku_description = "The Ellen DeGeneres Show
Watch with Three Interchangeable Straps";
// initialize log4j
final static Logger _logger =
Logger.getLogger(testEllenOrdersNew.class);
//public testEllenOrdersNewTest() {
//super();
//}
@Override
public void setUp() throws Exception {
//super();
System.out.println("Running test with base " + homepage);
setTestingEngineKey(TestingEngineRegistry.TESTING_ENGINE_HTMLUNIT);
getTestContext().setBaseUrl(homepage);
}
@Override
public void runTest() throws Exception {
setScriptingEnabled(false);
testSearchProduct();
testProductAdd();
testOrderAsRegisteredCustomer();
}
@Override
public void tearDown() throws Exception {
return;
}
static public class tHostnameVerifier implements HostnameVerifier {
public tHostnameVerifier() {
super();
}
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
// adds a product identified by sku
public void testSearchProduct() {
beginAt("/");
// this should run from any starting point, because it does a
search by the sku
//_logger.warn("first call to log4j...");
System.out.println("Searching for sku: " + sku);
System.out.println("Description: " + sku_description);
setTextField("keywords1", sku);
//getPageSource();
submit();
assertTextPresent(sku_description);
}
// adds a product identified by sku
public void testProductAdd() {
assertTextPresent(sku_description);
clickLinkWithText(sku_description);
System.out.print("Adding " + sku + " to cart........ ");
assertSubmitButtonPresent("add_to_cart");
submit("add_to_cart");
assertSubmitButtonPresent("ce_submit");
System.out.println("done");
//submit the cart page
System.out.print("Submitting Cart page....... ");
submit("ce_submit");
System.out.println("Done");
getPageSource();
}
/* to be used after form fillout
* goes though the rest of the shopcart process
*/
public void testOrderAsRegisteredCustomer() {
System.out.print("Logging in as [EMAIL PROTECTED]");
assertTextPresent("Why do I have to login?");
assertFormElementPresent("email_address");
setTextField("email_address", "[EMAIL PROTECTED]");
setTextField("login_password", "novator");
assertSubmitButtonPresent("login_submit");
submit("login_submit");
System.out.println("Done");
//review page
assertTextPresent("REVIEW YOUR ORDER INFORMATION CAREFULLY");
assertTextPresent("You have entered the credit card you would
like to be billed for your order.");
assertSubmitButtonPresent("review_submit");
System.out.print("Submitting review page.....");
submit("review_submit");
System.out.println("done");
//accept duties
System.out.print("Accepting Duties & Taxes.....");
assertTextPresent("Duties & Tax Acceptance");
assertSubmitButtonPresent("accept");
submit("accept");
System.out.println("done");
//confirm
assertTextPresent("Your order has been accepted");
assertTextPresent("Tracking your order is easy");
System.out.println("Order complete!");
}
public static void main(String args[]) throws
java.net.MalformedURLException, java.io.IOException {
HttpsURLConnection con = (HttpsURLConnection) new
URL("https://www.novator.com").openConnection();
con.setDefaultHostnameVerifier(new tHostnameVerifier());
// when running on Idea:
//junit.swingui.TestRunner.run(testEllenOrdersNew.class);
// when running on command line:
junit.textui.TestRunner.run(testEllenOrdersNew.class);
return;
}
}
Classpath is
lib/jwebunit-1.4/avalon-framework-4.1.3.jar:lib/jwebunit-1.4/commons-codec-1.3.jar:lib/jwebunit-1.4/commons-collections-3.2.jar:lib/jwebunit-1.4/commons-httpclient-3.0.1.jar:lib/jwebunit-1.4/commons-io-1.2.jar:lib/jwebunit-1.4/commons-lang-2.1.jar:lib/jwebunit-1.4/commons-logging-1.1.jar:lib/jwebunit-1.4/dom4j-1.6.1.jar:lib/jwebunit-1.4/htmlunit-1.10.jar:lib/jwebunit-1.4/icu4j-2.6.1.jar:lib/jwebunit-1.4/jaxen-1.1-beta-11.jar:lib/jwebunit-1.4/jdom-1.0.jar:lib/jwebunit-1.4/js-1.6R2.jar:lib/jwebunit-1.4/junit-3.8.2.jar:lib/jwebunit-1.4/jwebunit-core-1.4-rc3.jar:lib/jwebunit-1.4/jwebunit-htmlunit-plugin-1.4-rc3.jar:lib/jwebunit-1.4/log4j-1.2.12.jar:lib/jwebunit-1.4/logkit-1.0.1.jar:lib/jwebunit-1.4/nekohtml-0.9.5.jar:lib/jwebunit-1.4/regexp-1.3.jar:lib/jwebunit-1.4/servlet-api-2.4.jar:lib/jwebunit-1.4/tagsoup-0.9.7.jar:lib/jwebunit-1.4/xalan-2.6.0.jar:lib/jwebunit-1.4/xercesImpl-2.6.2.jar:lib/jwebunit-1.4/xml-apis-1.3.02.jar:lib/jwebunit-1.4/xmlParserAPIs-2.6.2.jar:lib/jwebunit
-1
.4/xom-1.0b3.jar:lib/jwebunit-1.4/log4j.properties:.
.Running test with base http://ellen.hanif.newline.projects.novator.com/
log4j:WARN No appenders could be found for logger
(com.gargoylesoftware.htmlunit.WebClient).
log4j:WARN Please initialize the log4j system properly.
Searching for sku: ELCFWATCH
Description: The Ellen DeGeneres Show Watch with Three Interchangeable
Straps
Adding ELCFWATCH to cart........ done
Submitting Cart page....... Done
Logging in as [EMAIL PROTECTED]
Submitting review page.....done
Accepting Duties & Taxes.....done
Order complete!
.Running test with base http://ellen.hanif.newline.projects.novator.com/
Searching for sku: ELCFWATCH
Description: The Ellen DeGeneres Show Watch with Three Interchangeable
Straps
Adding ELCFWATCH to cart........ done
Submitting Cart page....... Done
Logging in as [EMAIL PROTECTED]
Submitting review page.....done
Accepting Duties & Taxes.....done
Order complete!
.Running test with base http://ellen.hanif.newline.projects.novator.com/
Searching for sku: ELCFWATCH
Description: The Ellen DeGeneres Show Watch with Three Interchangeable
Straps
Adding ELCFWATCH to cart........ done
Submitting Cart page....... Done
Logging in as [EMAIL PROTECTED]
Submitting review page.....done
Accepting Duties & Taxes.....done
Order complete!
Time: 167.754
OK (3 tests)
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
JWebUnit-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-users
___________________________________________________________________________
Découvrez une nouvelle façon d'obtenir des réponses à toutes vos questions !
Profitez des connaissances, des opinions et des expériences des internautes sur
Yahoo! Questions/Réponses
http://fr.answers.yahoo.com-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
JWebUnit-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-users