Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Compare: EasyTest : An Introduction

Showing with 143 additions and 18 deletions.
  1. +143 −18 EasyTest-:-An-Introduction.md
View
161 EasyTest-:-An-Introduction.md
@@ -1,29 +1,154 @@
-EasyTest is a JUnit based test framework that has been created to provide convenient Data Driven Testing approach to its users.
+EasyTest is a Data Driven Testing Framework which is based on JUnit framework.
-Until now, we feel that JUnit does not really have any substantial support for performing Data Driven testing.
-Its Parameterized and Theories Runner both were started as some approach to perform data driven testing. But both of them have a lot of limitations as well as are not sufficient to meet the real world need of doing Data Driven testing with ease and simplicity.
+EasyTest aims to provide its users a convenient mechanism to perform Data Driven testing and at the same time keeping it intuitive.
-A lot has been written on JUnit's support for Parameterized and Theories runner and how they fall short of achieving their goal. You can find detailed analysis of Parameterized Runner and it's limitations here : http://www.kumaranuj.com/2012/08/junits-parameterized-runner-and-data.html
-and about Theories Runner and its limitations here : http://www.kumaranuj.com/2012/08/junit-theories-and-data-driven-testing.html
+For eg. a user of EasyTest framework can mark the test methods with @Test annotations, just like in JUnit. But unlike JUnit, a user can pass input data to the test method in the form of an XML(or Excel or CSV or custom defined) file and the framework will make sure that the right test method gets called for the right set of test data.
-EasyTest aims to provide a framework that is complete in all respect to be able to provide its user the ability to drive their tests using data that sits outside of the test case/class.
+This is just an overview and EasyTest provides a rich feature set for its users to perform various levels of testing that have become part of today's testing world.
-Some of the features of EasyTest are :
-1. It supports @Test annotations. This means that you do not have to switch to a new annotation if you are already using it in your test classes.
+Here are few things that EasyTest provides:
+* A user can externalize the test data in a file, instead of defining it in the test class itself. This keeps the test data and the test methods separate.
-2. It supports parameters to the test method. You can now write a test method like this:
-`@Test
- public void testWithParameters(
-@Param()LibraryId id , @Param(name="itemid") ItemId itemId)`
+Heres an example of how it can be done:
-3. It supports providing Test data externally to the test class in any file format(for eg. CSV, XML,EXCEL or even user defined) both at the class level as well as at the method level.
+ @RunWith(DataDrivenTestRunner.class)
+ @DataLoader(filePaths = { "org/easetech/data/testData.csv" })
+ public class TestClass {
+ .........
-4. It supports RegistryEditors to resolve parameters at runtime.
+As you can see above we are using @RunWith provided by JUnit and running it with EasyTest defined Runner called DataDrivenTestRunner. Additionally we have annotation @DataLoader that a user uses to define the input test data for the test class.
-5. It supports custom converters to convert a set of test data into user defined Objects in a consistent manner.
+* Another major shift away from traditional Testing frameworks is that EasyTest provides its users the ability to pass parameters to the test method. So a user can now define a test method like this:
-And many more.
-Explore our Getting Started page to learn how easy it is to get up and running with the EasyTest framework.
+ @Test
+ public void testWithInputParameter(@Param(name = "libraryId")Float libraryId) {
+ ..........
+ }
+@Param identifies for the EasyTest framework which input parameters needs to be passed to the input test method.
-### Happy Testing.
+* A user can also pass custom Registry Editor based property objects to the test method. So for example, if you have an Id object LibraryId that has a registry Editor associated with it, you can simply pass that object to the test method like this:
+ @Test
+ public void testWithRegistryPropertyParameter(@Param() LibraryId libraryId) {
+ ..........
+ }
+
+Note in this case you can omit the name of the parameter, if you want. You can find more details on how RegistryEditor support works in EasyTest on its wiki page.
+
+* A user can also pass a custom object to the test method even if it is not a Registry Editor based object. In that case, a user has to provide a custom converter that knows how to convert data into the custom object. Thus the test class will look like this:
+### Part 1: Register the converter with the EasyTest framework:
+
+ @BeforeClass
+ public static void setUp() {
+ //ItemConverter knows how a Map of key value pair can be converted into a custom Item Object.
+ ConverterManager.registerConverter(ItemConverter.class);
+ }
+### Part 2: Use the custom object in the test method:
+
+ @Test
+ public void testItemConverter(@Param()Item item) {
+ Assert.assertNotNull(item);
+
+ }
+
+* Sometimes, it becomes necessary to override test data at the method level. EasyTest provides that facility.
+
+ @RunWith(DataDrivenTestRunner.class)
+ @DataLoader(filePaths = { "getTestData.xls" }, loaderType = LoaderType.EXCEL)
+ public class TestConditionsSupportedByDataDrivenTest {
+
+ @Test
+ @DataLoader(loader = CustomObjectDataLoader.class)
+ public void testCustomDataLoader(@Param()Item item) {
+ Assert.assertNotNull(item);
+
+ }
+
+ @Test
+ public void testClassLevelDataLoader(@Param()Map<String, Object> inputData) {
+ ............
+
+ }
+
+In the above scenario, testClassLevelDataLoader test method will get its input test data from getTestData.xls file whereas testCustomDataLoader will get its input test data from CustomObjectDataLoader class.
+
+* EasyTest moves away from custom Testing frameworks in one more aspect. It provides its user with the facility to return the test data from the test method. this is extremely useful in cases whhere you want to compare the test results of the test methods that are run on different platforms. For eg. you are currently using JDBC for your DAO class and you have tests written for that. Now you decided to move towards JPA. You need to confirm that your application is behaving as expected. What you do is you first run your EasyTest enabled test methods against JDBC based implementation. then run your tests against JPA based implementation and compare the results.
+NOTE: EasyTest saves any data that the test method returns into the same file as was used by the test class to load the test data for that method.
+An example of how you can do it is:
+
+ @Test
+ @DataLoader(filePaths = { "org/easetech/data/test-combined.xls" }, loaderType = LoaderType.EXCEL)
+ public Item getDataWithReturnType(@Param(name = "libraryId")Float libraryId, @Param()
+ ItemId itemId) {
+ return new Item();
+ }
+In this case, the returned data will be written to the file org/easetech/data/test-combined.xls under the heading "ActualResult".
+
+* There are scenarios when a user wants to measure how much time his service is actually taking to return the test data. This is useful in cases where you want to do first level of performance analysis and need to get a rough idea on which services might be slow. EasyTest provides a convenient annotation(@Intercept) support for such a scenario.
+
+ @RunWith(DataDrivenTestRunner.class)
+ @DataLoader(filePaths = { "org/easetech/data/testExcelData.xls" }, loaderType = LoaderType.EXCEL)
+ public class TestExcelDataLoader {
+
+ @Intercept
+ public static RealItemService itemService = new RealItemService();
+
+What this does is it simply calls the EasyTestDefaultInterceptor which simply prints the time taken in nanosecods by the actual business service call on the console.
+
+Obviously, it would be useless, if it did only this much. Interestingly it gives a lot more facility to its users.
+
+A User can specify @Intercept(interceptor="MyCustomInterceptor.class") to specify the custom behavior that may be required in his particular scenario. Thus user can drive this annotation and thus gives them a lot of power.
+
+* Another thing that EasyTest prides in is displaying the user which test method is run with what input test data(both the input key and the input value). Thus in the IDE that supports JUnit, the test methods are displayed in the following format:
+ testMethodOne{libraryId=123, itemId=456}
+ testMethodOne{libraryId=908, itemId=897}
+ testMethodTwo{libraryId=123, itemId=87}
+
+Note that the first two methods have the same name but different input data. EasyTest handles creating extra and seperate test methods for separate test data.
+
+### Spring Support in EasyTest
+EasyTest supports Spring 3.0.6.RELEASE and above.
+
+Thus a user can use EasyTest framework's SpringTestRunner to run DataDrivenTest in Spring Integration test environment.
+
+A simple way to do it would be like this:
+
+ @RunWith(SpringTestRunner.class)
+ @ContextConfiguration(classes = { XmlBusinessConfig.class }, loader =AnnotationConfigContextLoader.class)
+ @DataLoader(filePaths = { "input-data.xml" }, loaderType = LoaderType.XML)
+ @TransactionConfiguration(transactionManager="transactionManager" )
+ @Transactional
+ public class TestSpringConfiguration {
+
+ @Autowired @Intercept(interceptor = EasyTestDefaultInterceptor.class)
+ public ItemService testSubject;
+
+
+ @BeforeClass
+ public static void setUp() {
+ PropertyEditorManager.registerEditor(ItemId.class, ItemIdEditor.class);
+ }
+
+ @Test
+ public void testSimple(){
+ ...........
+ }
+
+
+ @Test
+ public void getItemsDataUsingXMLLoader(@Param(name="searchText") String searchText, @Param(name="itemType") String itemType, @Param() ItemId itemId , @Param(name="expectedItems") int expectedItems) {
+ System.out.println(testSubject == null);
+ List<Item> items = testSubject.getItems(itemId, searchText, itemType);
+ Assert.assertNotNull(items);
+ Assert.assertEquals(expectedItems, items.size());
+
+ }
+ }
+
+You will notice that we are using Spring provided Annotations @ContextConfiguration , @TransactionConfiguration and @Transactional. And instead of using SpringJunit$ClassRunner, we are using EasyTest's SpringTestRunner. And we are using the same @DataLoader annotation to load our test data.
+this will enable Data Driven Testing in Spring managed projects. Thus if you want to migrate from your current Spring Tests to EasyTest, all you have to do is use SpringTestRunner and provide input test data using @DataLoader and you will be good to go.
+
+Also notic that we are using @Intercept annotation along with @Autowired annotation. EasyTest framework will make sure that the injected Spring bean gets instrumented one more time to intercept service methods for the end user as well. Isn't it amazing?
+
+This page gives an overview of potentially what all you can do with EasyTest. It does not yet give you the complete picture of what all it is capable of.
+For more indepth understanding, look at the WIKI pages for each of the functionality that EasyTest provides.
Something went wrong with that request. Please try again.