You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Vincent Massol <vm...@octo.fr> on 2001/03/18 08:51:25 UTC
[PROPOSAL] Struts Unit Test Guidelines
I think we need to give some guidelines for anyone who would like to
participate in writing unit tests for Struts. I'd like to start this process
by giving you my views on the subject and discuss it. When we have a final
agreement, we can put a web page on the struts web site.
Here are some of the rules/guidelines that I can see so far :
1) Put the test sources in jakarta-struts/src/test. For example if you want
to write a unit test for class org.apache.struts.action.ServletAction, you
woud put the test class in jakarta-struts/src/test/org/apache/struts/action
2) Name of the test class. For any XXX class to test, the associated test
class should be named TestXXX. I'm sure others will prefer XXXTest. I don't
really care, we just need to standardize on one.
3) Any helper class and resources for the test should be put in the same
directory as the test class. For example, if my test class accepts several
xml files, I would put them where I put my TestXXX class and I would use
Class.getResourceAsStream() to load them (thus removing the need to hard
code the path of the resource). Again, this is a proposition. Another
solution would be to put them in the jakara-struts/conf/test directory.
4) All test suites should be gathered in a single TestAll.java (or
AllTest.java) test class that will located in
jakarta-struts/src/test/org/apache/struts. The build.xml target for unit
tests will use this file to run all the tests.
And then general good practice rules (See this article for more details
http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit.html) :
5) Test cases should be completely independent from one another. Don't
assume the order in which your test cases will run.
6) Avoid hard coding paths in your test cases
7) do not use the test case constructor to set up a test case
8) Avoid writing test cases with side-effects
9) Keep tests small and fast
10) Name test cases properly
11) Ensure that tests are time independent
12) Consider locale when writing tests
13) Always test for error conditions
14) Utilize properly assert/fails and exception mechanism (i.e. don't put a
ty/catch around a test case for which you don't expect an exception)
15) Document tests in javadoc. This is very important as unit tests are a
very good way to document the code.
Comments ? (I'm sure I'll get plenty ... :)
Vincent.
Re: [PROPOSAL] Struts Unit Test Guidelines
Posted by Vincent Massol <vm...@octo.fr>.
----- Original Message -----
From: "Martin Cooper" <ma...@tumbleweed.com>
To: <st...@jakarta.apache.org>
Sent: Monday, March 19, 2001 7:52 AM
Subject: Re: [PROPOSAL] Struts Unit Test Guidelines
> Please see comments inline.
>
> ----- Original Message -----
> From: "Vincent Massol" <vm...@octo.fr>
> To: <st...@jakarta.apache.org>
> Sent: Saturday, March 17, 2001 11:51 PM
> Subject: [PROPOSAL] Struts Unit Test Guidelines
>
>
> > I think we need to give some guidelines for anyone who would like to
> > participate in writing unit tests for Struts. I'd like to start this
> process
> > by giving you my views on the subject and discuss it. When we have a
final
> > agreement, we can put a web page on the struts web site.
> >
> > Here are some of the rules/guidelines that I can see so far :
> >
> > 1) Put the test sources in jakarta-struts/src/test. For example if you
> want
> > to write a unit test for class org.apache.struts.action.ServletAction,
you
> > woud put the test class in
> jakarta-struts/src/test/org/apache/struts/action
>
> I had been thinking of a "sub-package" for tests (e.g.
> <original-package-name>.test), but now that I think about it, it makes
sense
> to put the tests in the same package, for access reasons.
>
> > 2) Name of the test class. For any XXX class to test, the associated
test
> > class should be named TestXXX. I'm sure others will prefer XXXTest. I
> don't
> > really care, we just need to standardize on one.
>
> I would prefer TestXXX, for two reasons:
>
> 1) It matches the way I think about unit tests ;-) (i.e. "test this,
> please")
> 2) It is consistent with JUnit's automatic identification of tests using
> testXXX() methods.
>
> > 3) Any helper class and resources for the test should be put in the same
> > directory as the test class. For example, if my test class accepts
several
> > xml files, I would put them where I put my TestXXX class and I would use
> > Class.getResourceAsStream() to load them (thus removing the need to hard
> > code the path of the resource). Again, this is a proposition. Another
> > solution would be to put them in the jakara-struts/conf/test directory.
>
> When I was thinking about "sub-packages" for testing, I had been thinking
> about a ".testdata" sub-package. I still think a separate package for test
> data would be useful. It would help isolate data used as input to the
tests
> from data required by the test code itself, and isolate both of those from
> data in the package under test.
>
Why not, but then it must be unique enough so that this package does not
exist in the src packages ? Or put the helper classes in the same dir and
helper config files in conf/test ?
> My thinking is that test data for the
org.apache.struts.action.ServletAction
> class would be found in
> jakarta-struts/src/test/org/apache/struts/action/testdata/.
>
> I agree completely with using Class.getResourceAsStream() to access the
test
> data. That's what I'm doing now. :-)
>
> > 4) All test suites should be gathered in a single TestAll.java (or
> > AllTest.java) test class that will located in
> > jakarta-struts/src/test/org/apache/struts. The build.xml target for unit
> > tests will use this file to run all the tests.
>
> To the extent that this can be done automatically, I agree. However, I
think
> we should do as much as possible to minimize the effort required to add a
> new test.
>
> For example, with JUnit, you can add a test just by adding a method named
> "testXXX" to your class derived from TestCase. If we can define a Struts
> test suite in a similar way, perhaps using JUnit constructs or perhaps
using
> Ant build files (which are probably more flexible), that would be great.
>
yes I agree. See my response to Craig's email on the same subject
> > And then general good practice rules (See this article for more details
> > http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit.html) :
> >
> > 5) Test cases should be completely independent from one another. Don't
> > assume the order in which your test cases will run.
> > 6) Avoid hard coding paths in your test cases
> > 7) do not use the test case constructor to set up a test case
> > 8) Avoid writing test cases with side-effects
> > 9) Keep tests small and fast
> > 10) Name test cases properly
> > 11) Ensure that tests are time independent
> > 12) Consider locale when writing tests
> > 13) Always test for error conditions
> > 14) Utilize properly assert/fails and exception mechanism (i.e. don't
put
> a
> > ty/catch around a test case for which you don't expect an exception)
> > 15) Document tests in javadoc. This is very important as unit tests are
a
> > very good way to document the code.
>
> These seem like good guidelines to me!
>
> > Vincent.
>
> --
> Martin Cooper
>
>
>
>
Vincent
Re: [PROPOSAL] Struts Unit Test Guidelines
Posted by Martin Cooper <ma...@tumbleweed.com>.
Please see comments inline.
----- Original Message -----
From: "Vincent Massol" <vm...@octo.fr>
To: <st...@jakarta.apache.org>
Sent: Saturday, March 17, 2001 11:51 PM
Subject: [PROPOSAL] Struts Unit Test Guidelines
> I think we need to give some guidelines for anyone who would like to
> participate in writing unit tests for Struts. I'd like to start this
process
> by giving you my views on the subject and discuss it. When we have a final
> agreement, we can put a web page on the struts web site.
>
> Here are some of the rules/guidelines that I can see so far :
>
> 1) Put the test sources in jakarta-struts/src/test. For example if you
want
> to write a unit test for class org.apache.struts.action.ServletAction, you
> woud put the test class in
jakarta-struts/src/test/org/apache/struts/action
I had been thinking of a "sub-package" for tests (e.g.
<original-package-name>.test), but now that I think about it, it makes sense
to put the tests in the same package, for access reasons.
> 2) Name of the test class. For any XXX class to test, the associated test
> class should be named TestXXX. I'm sure others will prefer XXXTest. I
don't
> really care, we just need to standardize on one.
I would prefer TestXXX, for two reasons:
1) It matches the way I think about unit tests ;-) (i.e. "test this,
please")
2) It is consistent with JUnit's automatic identification of tests using
testXXX() methods.
> 3) Any helper class and resources for the test should be put in the same
> directory as the test class. For example, if my test class accepts several
> xml files, I would put them where I put my TestXXX class and I would use
> Class.getResourceAsStream() to load them (thus removing the need to hard
> code the path of the resource). Again, this is a proposition. Another
> solution would be to put them in the jakara-struts/conf/test directory.
When I was thinking about "sub-packages" for testing, I had been thinking
about a ".testdata" sub-package. I still think a separate package for test
data would be useful. It would help isolate data used as input to the tests
from data required by the test code itself, and isolate both of those from
data in the package under test.
My thinking is that test data for the org.apache.struts.action.ServletAction
class would be found in
jakarta-struts/src/test/org/apache/struts/action/testdata/.
I agree completely with using Class.getResourceAsStream() to access the test
data. That's what I'm doing now. :-)
> 4) All test suites should be gathered in a single TestAll.java (or
> AllTest.java) test class that will located in
> jakarta-struts/src/test/org/apache/struts. The build.xml target for unit
> tests will use this file to run all the tests.
To the extent that this can be done automatically, I agree. However, I think
we should do as much as possible to minimize the effort required to add a
new test.
For example, with JUnit, you can add a test just by adding a method named
"testXXX" to your class derived from TestCase. If we can define a Struts
test suite in a similar way, perhaps using JUnit constructs or perhaps using
Ant build files (which are probably more flexible), that would be great.
> And then general good practice rules (See this article for more details
> http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit.html) :
>
> 5) Test cases should be completely independent from one another. Don't
> assume the order in which your test cases will run.
> 6) Avoid hard coding paths in your test cases
> 7) do not use the test case constructor to set up a test case
> 8) Avoid writing test cases with side-effects
> 9) Keep tests small and fast
> 10) Name test cases properly
> 11) Ensure that tests are time independent
> 12) Consider locale when writing tests
> 13) Always test for error conditions
> 14) Utilize properly assert/fails and exception mechanism (i.e. don't put
a
> ty/catch around a test case for which you don't expect an exception)
> 15) Document tests in javadoc. This is very important as unit tests are a
> very good way to document the code.
These seem like good guidelines to me!
> Vincent.
--
Martin Cooper
Re: [PROPOSAL] Struts Unit Test Guidelines
Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Mon, 19 Mar 2001, Vincent Massol wrote:
>
> ----- Original Message -----
> From: "Craig R. McClanahan" <cr...@apache.org>
> To: <st...@jakarta.apache.org>
> Sent: Sunday, March 18, 2001 11:39 PM
> Subject: Re: [PROPOSAL] Struts Unit Test Guidelines
>
>
> >
> >
> > On Sun, 18 Mar 2001, Vincent Massol wrote:
> >
> > > I think we need to give some guidelines for anyone who would like to
> > > participate in writing unit tests for Struts. I'd like to start this
> process
> > > by giving you my views on the subject and discuss it. When we have a
> final
> > > agreement, we can put a web page on the struts web site.
> > >
> > > Here are some of the rules/guidelines that I can see so far :
> > >
> > > 1) Put the test sources in jakarta-struts/src/test. For example if you
> want
> > > to write a unit test for class org.apache.struts.action.ServletAction,
> you
> > > woud put the test class in
> jakarta-struts/src/test/org/apache/struts/action
> > >
> >
> > An implication of this is that the package would be
> > "org.apache.struts.action", the same as the package you are testing, which
> > allows access to package private methods. We might want to state that
> > explicitly as a guideline.
> >
>
> sure
>
> > > 2) Name of the test class. For any XXX class to test, the associated
> test
> > > class should be named TestXXX. I'm sure others will prefer XXXTest. I
> don't
> > > really care, we just need to standardize on one.
> > >
> >
> > I have a mild preference for "TextXxxxx" -- I think of the word test as a
> > "verb", and that is what "test xxxxx" would mean (at least in English :-).
>
> What do you propose ?
>
TestActionServlet for the tests that exercise ActionServlet, and so on.
> >
> > I also like naming the test methods "testThisCase()" or
> > "testThatMethod()" as well.
>
> yes, but testThatMethod() is not enough. For a given method there are
> several test cases : one with such bad argument, another with a bad
> environment set up, ... So, I 'd rather state it as in 10) below (see
> http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit.html) like :
> testThatMethodNullPath()
>
You're right, of course. The individual test names should include both
the method being tested and some indication of what you are testing for.
> >
> > > 3) Any helper class and resources for the test should be put in the same
> > > directory as the test class. For example, if my test class accepts
> several
> > > xml files, I would put them where I put my TestXXX class and I would use
> > > Class.getResourceAsStream() to load them (thus removing the need to hard
> > > code the path of the resource). Again, this is a proposition. Another
> > > solution would be to put them in the jakara-struts/conf/test directory.
> > >
> >
> > +1. This concept should be familiar to Struts developers anyway, because
> > that is how application resources (message bundles) are accessed.
> >
>
> fine. What about locations of helper classes and resources ? Do we want to
> standardize on their naming also. What I do on my projects is to prefix all
> of them with the name of the test class that need them. For example :
> TestXXX_Helper.class, TestXXX_BadConfiguration.xml, ... But I understand
> that you might not like this. The problem is you don't do that you might
> find yourselves with tens or hundreds or resources files and classes and you
> don' know who is using them. This is especially true if the class to test
> use a properties or an xml file. You might want to test your class against
> several bad formatted config files to verify that you handle correctly
> exceptions cases. Another solution as Martin suggested is to put them
> somewhere in conf/test/
>
I'm fine with TestXXX_Helper.java, TestXXX_BadConfiguration.xml, and so on
being in the same directory as the test classes themselves, for the
following reasons:
* I have a harder time finding dependent things if they are not
all in the same directory. Because of the common name prefix
(TestXXX), I can tell that all of these things belong together.
* If they are in the same directory, the sorted listing even puts
them all together in a nice order.
> > > 4) All test suites should be gathered in a single TestAll.java (or
> > > AllTest.java) test class that will located in
> > > jakarta-struts/src/test/org/apache/struts. The build.xml target for unit
> > > tests will use this file to run all the tests.
> > >
> >
> > (I'm assuming a missing guideline that the tests will be run from an Ant
> > script of some sort - if that assumption is wrong, the following will not
> > make much sense.)
> >
> > Do we really need a TestAll class if we're using Ant to run the tests? We
> > can easily create targets for each suite of tests you might want to run
> > together, and have an "all" target that causes all of them to be executed
> > through appropriate dependencies.
> >
> > Given that you have to update the Ant testing script anyway as new tests
> > are added, it seems redundant to also have to modify TestAll.
>
> yes I agree. Let's forget the TestAll java class. Also, the junit ant task
> has a <batch> nested tag to automatically look for classes that extend
> TestCase and run them ...
Cool.
> >
> > > And then general good practice rules (See this article for more details
> > > http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit.html) :
> > >
> > > 5) Test cases should be completely independent from one another. Don't
> > > assume the order in which your test cases will run.
> > > 6) Avoid hard coding paths in your test cases
> > > 7) do not use the test case constructor to set up a test case
> > > 8) Avoid writing test cases with side-effects
> > > 9) Keep tests small and fast
> > > 10) Name test cases properly
> > > 11) Ensure that tests are time independent
> > > 12) Consider locale when writing tests
> > > 13) Always test for error conditions
> > > 14) Utilize properly assert/fails and exception mechanism (i.e. don't
> put a
> > > ty/catch around a test case for which you don't expect an exception)
> > > 15) Document tests in javadoc. This is very important as unit tests are
> a
> > > very good way to document the code.
> > >
> > > Comments ? (I'm sure I'll get plenty ... :)
> > >
> > > Vincent.
> > >
> > >
> >
> > Craig
> >
> >
> >
> Vincent
>
>
Craig
Re: [PROPOSAL] Struts Unit Test Guidelines
Posted by Vincent Massol <vm...@octo.fr>.
----- Original Message -----
From: "Craig R. McClanahan" <cr...@apache.org>
To: <st...@jakarta.apache.org>
Sent: Sunday, March 18, 2001 11:39 PM
Subject: Re: [PROPOSAL] Struts Unit Test Guidelines
>
>
> On Sun, 18 Mar 2001, Vincent Massol wrote:
>
> > I think we need to give some guidelines for anyone who would like to
> > participate in writing unit tests for Struts. I'd like to start this
process
> > by giving you my views on the subject and discuss it. When we have a
final
> > agreement, we can put a web page on the struts web site.
> >
> > Here are some of the rules/guidelines that I can see so far :
> >
> > 1) Put the test sources in jakarta-struts/src/test. For example if you
want
> > to write a unit test for class org.apache.struts.action.ServletAction,
you
> > woud put the test class in
jakarta-struts/src/test/org/apache/struts/action
> >
>
> An implication of this is that the package would be
> "org.apache.struts.action", the same as the package you are testing, which
> allows access to package private methods. We might want to state that
> explicitly as a guideline.
>
sure
> > 2) Name of the test class. For any XXX class to test, the associated
test
> > class should be named TestXXX. I'm sure others will prefer XXXTest. I
don't
> > really care, we just need to standardize on one.
> >
>
> I have a mild preference for "TextXxxxx" -- I think of the word test as a
> "verb", and that is what "test xxxxx" would mean (at least in English :-).
What do you propose ?
>
> I also like naming the test methods "testThisCase()" or
> "testThatMethod()" as well.
yes, but testThatMethod() is not enough. For a given method there are
several test cases : one with such bad argument, another with a bad
environment set up, ... So, I 'd rather state it as in 10) below (see
http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit.html) like :
testThatMethodNullPath()
>
> > 3) Any helper class and resources for the test should be put in the same
> > directory as the test class. For example, if my test class accepts
several
> > xml files, I would put them where I put my TestXXX class and I would use
> > Class.getResourceAsStream() to load them (thus removing the need to hard
> > code the path of the resource). Again, this is a proposition. Another
> > solution would be to put them in the jakara-struts/conf/test directory.
> >
>
> +1. This concept should be familiar to Struts developers anyway, because
> that is how application resources (message bundles) are accessed.
>
fine. What about locations of helper classes and resources ? Do we want to
standardize on their naming also. What I do on my projects is to prefix all
of them with the name of the test class that need them. For example :
TestXXX_Helper.class, TestXXX_BadConfiguration.xml, ... But I understand
that you might not like this. The problem is you don't do that you might
find yourselves with tens or hundreds or resources files and classes and you
don' know who is using them. This is especially true if the class to test
use a properties or an xml file. You might want to test your class against
several bad formatted config files to verify that you handle correctly
exceptions cases. Another solution as Martin suggested is to put them
somewhere in conf/test/
> > 4) All test suites should be gathered in a single TestAll.java (or
> > AllTest.java) test class that will located in
> > jakarta-struts/src/test/org/apache/struts. The build.xml target for unit
> > tests will use this file to run all the tests.
> >
>
> (I'm assuming a missing guideline that the tests will be run from an Ant
> script of some sort - if that assumption is wrong, the following will not
> make much sense.)
>
> Do we really need a TestAll class if we're using Ant to run the tests? We
> can easily create targets for each suite of tests you might want to run
> together, and have an "all" target that causes all of them to be executed
> through appropriate dependencies.
>
> Given that you have to update the Ant testing script anyway as new tests
> are added, it seems redundant to also have to modify TestAll.
yes I agree. Let's forget the TestAll java class. Also, the junit ant task
has a <batch> nested tag to automatically look for classes that extend
TestCase and run them ...
>
> > And then general good practice rules (See this article for more details
> > http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit.html) :
> >
> > 5) Test cases should be completely independent from one another. Don't
> > assume the order in which your test cases will run.
> > 6) Avoid hard coding paths in your test cases
> > 7) do not use the test case constructor to set up a test case
> > 8) Avoid writing test cases with side-effects
> > 9) Keep tests small and fast
> > 10) Name test cases properly
> > 11) Ensure that tests are time independent
> > 12) Consider locale when writing tests
> > 13) Always test for error conditions
> > 14) Utilize properly assert/fails and exception mechanism (i.e. don't
put a
> > ty/catch around a test case for which you don't expect an exception)
> > 15) Document tests in javadoc. This is very important as unit tests are
a
> > very good way to document the code.
> >
> > Comments ? (I'm sure I'll get plenty ... :)
> >
> > Vincent.
> >
> >
>
> Craig
>
>
>
Vincent
RE: [PROPOSAL] Struts Unit Test Guidelines
Posted by Michael Hackett <mh...@pictorius.com>.
> From: Craig R. McClanahan <cr...@apache.org>
> I have a mild preference for "TextXxxxx" -- I think of the
> word test as a
> "verb", and that is what "test xxxxx" would mean (at least in
> English :-).
The word "test" is just as likely to be a noun. Didn't you take any
"tests" in school? :-) Also, class names are supposed to be nouns,
although it seems the usual convention for JUnit is to use Test as a
prefix. (Probably the most correct form would be XxxTester, but the
convention is established now and will be hard to break.)
--
Michael Hackett
Developer, Pictorius Inc.
Re: [PROPOSAL] Struts Unit Test Guidelines
Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Sun, 18 Mar 2001, Vincent Massol wrote:
> I think we need to give some guidelines for anyone who would like to
> participate in writing unit tests for Struts. I'd like to start this process
> by giving you my views on the subject and discuss it. When we have a final
> agreement, we can put a web page on the struts web site.
>
> Here are some of the rules/guidelines that I can see so far :
>
> 1) Put the test sources in jakarta-struts/src/test. For example if you want
> to write a unit test for class org.apache.struts.action.ServletAction, you
> woud put the test class in jakarta-struts/src/test/org/apache/struts/action
>
An implication of this is that the package would be
"org.apache.struts.action", the same as the package you are testing, which
allows access to package private methods. We might want to state that
explicitly as a guideline.
> 2) Name of the test class. For any XXX class to test, the associated test
> class should be named TestXXX. I'm sure others will prefer XXXTest. I don't
> really care, we just need to standardize on one.
>
I have a mild preference for "TextXxxxx" -- I think of the word test as a
"verb", and that is what "test xxxxx" would mean (at least in English :-).
I also like naming the test methods "testThisCase()" or
"testThatMethod()" as well.
> 3) Any helper class and resources for the test should be put in the same
> directory as the test class. For example, if my test class accepts several
> xml files, I would put them where I put my TestXXX class and I would use
> Class.getResourceAsStream() to load them (thus removing the need to hard
> code the path of the resource). Again, this is a proposition. Another
> solution would be to put them in the jakara-struts/conf/test directory.
>
+1. This concept should be familiar to Struts developers anyway, because
that is how application resources (message bundles) are accessed.
> 4) All test suites should be gathered in a single TestAll.java (or
> AllTest.java) test class that will located in
> jakarta-struts/src/test/org/apache/struts. The build.xml target for unit
> tests will use this file to run all the tests.
>
(I'm assuming a missing guideline that the tests will be run from an Ant
script of some sort - if that assumption is wrong, the following will not
make much sense.)
Do we really need a TestAll class if we're using Ant to run the tests? We
can easily create targets for each suite of tests you might want to run
together, and have an "all" target that causes all of them to be executed
through appropriate dependencies.
Given that you have to update the Ant testing script anyway as new tests
are added, it seems redundant to also have to modify TestAll.
> And then general good practice rules (See this article for more details
> http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit.html) :
>
> 5) Test cases should be completely independent from one another. Don't
> assume the order in which your test cases will run.
> 6) Avoid hard coding paths in your test cases
> 7) do not use the test case constructor to set up a test case
> 8) Avoid writing test cases with side-effects
> 9) Keep tests small and fast
> 10) Name test cases properly
> 11) Ensure that tests are time independent
> 12) Consider locale when writing tests
> 13) Always test for error conditions
> 14) Utilize properly assert/fails and exception mechanism (i.e. don't put a
> ty/catch around a test case for which you don't expect an exception)
> 15) Document tests in javadoc. This is very important as unit tests are a
> very good way to document the code.
>
> Comments ? (I'm sure I'll get plenty ... :)
>
> Vincent.
>
>
Craig