There are two aspects which might impact the way how you structure your tests within classes.
- Readability. You might want to use classes as logical boundaries so that the names of your classes would represent some area the tests inside those classes relate to. If you use the classes just because you have to implement your tests within classes and use them as test "folders" then it is up to you how you think it would be more convenient to housekeep your code
- Using the power of OOP tool set. You might want to power up your tests with applying OOP conception to your code. In this way you might structure your tests using inheritance concept. Thus your will have some classes that would extend some basic classes with basic tests and add more specific tests to the particular area. You might also apply polymorphism to distinguish the cases when you need to run different code for similar actions. Here is my example for Java:
Base Code:
public class Base {
public void prepare(){
System.out.println("Basic prepare");
}
@Test
public void testInBase1(){
prepare();
System.out.println("Test in Base 1 executed");
}
@Test
public void testInBase2(){
prepare();
System.out.println("Test in Base 2 executed");
}
}
Code that would execute overriden preparation and add specific test set:
public class Extension1 extends Base{
@Override
public void prepare() {
System.out.println("Extended prepare");
}
@Test
public void testInExtension11(){
prepare();
System.out.println("Test in extension 11 executed");
}
@Test
public void testInExtension12(){
prepare();
System.out.println("Test in extension 12 executed");
}
}
Code that would execute basic preparation and add specific test set:
public class Extension2 extends Base{
@Test
public void testInExtension21(){
prepare();
System.out.println("Test in extension 21 executed");
}
}
So in this structure if you run Extension1
class you would run all the tests from Base
+ all the tests from Extension1
and prepare
code will be taken from Extension1
even for the tests from Base
(because of polymorphism).
If you run the class Extension2
then you would run Base+Extended test set but with prepare
code taken from Base
.
When you use OOP approach you should consider the particular test execution framework you are using. For example JUnit requires all the tests in Base class are to be public
howver TestNG
can run even private
tests.