TNG / junit-dataprovider

A TestNG like dataprovider runner for JUnit with many additional features
Apache License 2.0
246 stars 164 forks source link

Add @BeforeDataProvider and @AfterDataProvider methods #82

Closed aaschmid closed 8 years ago

aaschmid commented 8 years ago

Taken from #79, asked by @Vivek-Malhotra:

Regarding the use case in @Before I am not sure whether that is correct as I am pretty new to spring. What I though from my previous experience on other Test Frameworks, I used to do any prerequisites for a test in a setup script.

Similarly, there should be an option to use test data from test data provider in @Before.

For e.g. I have a use case, where my Test Need some XML Invoices which are coming from a Mock. I am starting the Mock in @Before and would like to pass the test data which my Mock needs in @Before.

Then in @Before I am hosting the mock with the XML invoices from Test Data Provider. Then I am adding some Test Data in some DB based on the Data from Test Data provider. All this is prerequisite for my test to run. Since it doesn't work in @Before. I am simply doing in in @Test at the moment and that's fine. However, it would have been nice to have similar feature in @Before.

What are your views on this?

aaschmid commented 8 years ago

Hi @Vivek-Malhotra,

I think it is possible to implement if I understand you corrently. However, as I am now thinking more detailed about it, I have some doubt about the usefullness. I am currently not convinced what the benefits would be because it would cause some additional boiler plate code to add a second method for every test case annotated e.g. with @BeforeDataProvider("dataProviderName"). And as you can put the setup stuff into your @Test method where it also occurs only once, you already safe a lot of duplicated code ...

Do you understand what I mean? (You already implemented it like that :-) )

Cheers, Andreas

P.S.: What I already discussed some time ago is a method which is called before the first method of a certain dataprovider and after the last method of the same dataprovider to run...

Vivek-Malhotra commented 8 years ago

Hi @aaschmid Agreed on your point. I can have my setup inside @Test where I can provide test data from Data Provider.

Why I thought it would be good to add Test Data in @Before was in case I have several test methods in my class which needs same test data, I have to use this data provider in every test method (@Test). It would have been good if I provide Test Data in @Before and then all my tests use the same data.

But anyways its not that important as currently my purpose is solved and I am not sure if using test data in @Before is a correct approach as you mentioned about boiler plate code.

Also, you mentioned about @BeforeDataProvider("dataProviderName"). What is that. Is this some additional annotation provided by your data provider?

Thanks, Vivek

aaschmid commented 8 years ago

Hi @Vivek-Malhotra, yes this is the correct use case to add the test data for every test case in @Before. The @BeforeDataProvider("dataProviderName") was just a name suggestion for your idea. The more I think about it, I think it would be a good idea to try to create something like @BeforeDataProvider("dataProviderName") and @AfterDataProvider("dataProviderName") which is executed before the first test using the dataprovider with name "dataProviderName" and once after the last test ... (without being able to access the dataprovider data - at least for a first version).

Would that make sense to you? Do you see any benefit for that?

Cheers, Andreas

aaschmid commented 8 years ago

I implemented already some part of it because I was curious about if this would work. And yes, it would work but due to the bad extensibility of JUnit it is a bit hacky ...

Anybody would like to have that feature?

Vivek-Malhotra commented 8 years ago

Hi @aaschmid ,

This is really great that you already tried to make it work. That’s super fast I can say 

I am not sure how useful it would be to put these efforts against what we already have. Some of the use cases which I can think of (based on my lame experience with Junit) are as below:

Please let me know what you think:

Use Case I: I have one DataProvider Class having One DataProvider for One Test Class having n Number of Tests

One Data Provider Class - JunitDataProvider

_@Dataprovider Public static Object [][] testDataForMyTestA(){ }_

One Spring Junit Test Class – With n Tests

@RunWith(DataProviderRunnerWithSpring.class) @SpringApplicationConfiguration(TestConfiguration.class) public class MyTestA extends MyBaseTest {

@Test @UseDataProvider(value = " testDataForMyTestA ", location = JunitDataProvider.class) public void testMethod1(String invoice1, String invoice2) throws Exception { }

@Test @UseDataProvider(value = " testDataForMyTestA ", location = JunitDataProvider.class) public void testMethod2(String invoice1, String invoice2) throws Exception { } }

**Use Case II: I have one DataProvider Class having n DataProviders for One Test Class having n Number of Tests

One Data Provider Class having n DataProviders - JunitDataProvider**

@Dataprovider Public static Object [][] testDataForMyTestA(){ }

@Dataprovider Public static Object [][] testDataForMyTestB(){ }

One Spring Junit Test Class – With n Tests

@RunWith(DataProviderRunnerWithSpring.class) @SpringApplicationConfiguration(TestConfiguration.class) public class MyTestA extends MyBaseTest {

@Test @UseDataProvider(value = " testDataForMyTestA ", location = JunitDataProvider.class) public void testMethod1(String invoice1, String invoice2) throws Exception { }

@Test @UseDataProvider(value = " testDataForMyTestB ", location = JunitDataProvider.class) public void testMethod2(String invoice1, String invoice2) throws Exception { } }

Use Case III: I have one DataProvider Class having n DataProviders for Different Test Classes

One Data Provider Class having n DataProviders - JunitDataProvider

@Dataprovider Public static Object [][] testDataForMyTestA(){ }

@Dataprovider Public static Object [][] testDataForMyTestB(){ }

Multiple Spring Junit Test Class consuming corresponding DataProviders

@RunWith(DataProviderRunnerWithSpring.class) @SpringApplicationConfiguration(TestConfiguration.class) public class MyTestA extends MyBaseTest {

@Test @UseDataProvider(value = " testDataForMyTestA ", location = JunitDataProvider.class) public void testMethod1(String invoice1, String invoice2) throws Exception { } }

@RunWith(DataProviderRunnerWithSpring.class) @SpringApplicationConfiguration(TestConfiguration.class) public class MyTestB extends MyBaseTest {

@Test @UseDataProvider(value = " testDataForMyTestB ", location = JunitDataProvider.class) public void testMethod1(String invoice1, String invoice2) throws Exception { } }

Regards Vivek Malhotra

aaschmid commented 8 years ago

Hi @Vivek-Malhotra, I am sorry but I am not sure what that has to do with the @BeforeDataProvider and / or @AfterDataProvider ...

aaschmid commented 8 years ago

While thinking in more depth about it, it makes no sence to have a @BeforeDataProvider("dataProviderName") and @AfterDataProvider("dataProviderName") because

So I will close this issue

aaschmid commented 8 years ago

Former tries, see https://github.com/Airblader/junit-dataprovider/commit/a778c7e36b4e5dcc61fa9e60f8eb25be92ee66a2