0

I have a site programmed in PHP where I make more call to external server that return me big xml of data. For each record of the xml I have to make other external call to get other data.

So my function logic is:

1) make external request in XML

2) get the response XML

3) for each record of the XML make another external request in XML

So there are three functions to get all data for my page. In theory to make good tests I need to create a test for each function, but the problem is that every function is linked with the other function because I need record data to make the last function.

So is correct to create a big test that include all functions in sequence to have all data to make all functions or is better another way that I don't know?

I make test with PHPUnit but I would like to know if this practice is a good practice or there is another way to make good test for this feature.

Consider that I make more external request (Point 3) not only one so I have more functions to get all data that I need (I have already cut data that I don't need).

Thanks

marked as duplicate by gnat, user53019, Arseni Mourzenko, user40980, durron597 Mar 27 '15 at 12:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

Ideally, you want both unit tests (which test individual functions) and integration tests (which test all the functions together), not just one or the other. You will also need a way to mock out the web service you're calling so that these tests don't depend on it.

To directly address a few of the sub-questions:

So is correct to create a big test that include all functions in sequence to have all data to make all functions or is better another way that I don't know?

Yes, that's called an integration test. It is not correct to rely exclusively on integration tests, but you definitely want some of them.

I make test with PHPUnit but I would like to know if this practice is a good practice or there is another way to make good test for this feature.

This part is essentially a software recommendation question so it's technically off-topic here, but from what I can tell that framework does have a way to mock web services, declare how many calls the mock service should expect and what it should return each time, so it should do the job. In general, using a testing framework for your tests is almost always a good idea, since the alternative is writing and maintaining a lot of service mocking code and error message presentation code yourself.

  • Thanks for your answer! its' very clear and I have understand the point! I'll try it – Alessandro Minoccheri Mar 25 '15 at 9:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.