Skip to main content
11 votes
Accepted

Unit Testing: Method per test versus data provider

It depends. You should ask yourself: what property of the code is this particular test testing? What do I know when I know that this test is green? I don't mean this as a philosophical exercise, but ...
wallenborn's user avatar
  • 1,980
10 votes

Writing unit test cases are taking time, any advice?

Writing test cases generally take a long time if you don't use test-driven development, because of several factors which are explained very well in Working Effectively with Legacy Code. In your ...
l0b0's user avatar
  • 11.5k
8 votes
Accepted

Testing Queries Themselves with Test data is a Unit test or an Integration test?

Purists won't call any test a "unit test" which involves a database. Other purists won't call a test an "integration test" when there are not at least two components involved (and a platform like the ...
Doc Brown's user avatar
  • 212k
7 votes
Accepted

Writing unit test cases are taking time, any advice?

It is not unusual for writing good tests to take considerable time - strictly you should be able to write your tests just from the specification and interface that is what is done in some formal test ...
Steve Barnes's user avatar
  • 5,320
7 votes

How to write a unit test for a repository method that returns some data, where data can change over time?

If the whole purpose of a method is to get some data from a database, it it obviously useless to write an automatic test for the method which mocks the database away, that would thwart the whole ...
Doc Brown's user avatar
  • 212k
5 votes

Unit Testing: Method per test versus data provider

Many of your points come from weaknesses of your testing framework. If we look at Spock, it addresses a number of your concerns. Let's see how to write your test in Spock @Unroll final "add #a and #b ...
walpen's user avatar
  • 3,241
4 votes

How to unit test database queries based on current date?

Make the functions that should use "current date" receive a parameter for it, and default it to current date. Something like below: public function deletePastEntries($now = null) { if (! $now) { ...
9000's user avatar
  • 24.3k
3 votes

Bad Practice - having a SUT use a different execution path when in test mode?

Yeah it is a bit bad. However, let play devil's advocate. Unit testing code which interacts with a database is difficult. I guess the most popular approach would be to add an in memory database to ...
Ewan's user avatar
  • 78.5k
3 votes
Accepted

Unit testing for non-stateless units - how?

Testing is more than unit testing. Testing can be as simple as running the code and playing with the app. It can be as intense as a suite of tests ran before you're allowed to merge code into a ...
candied_orange's user avatar
3 votes

Why is automatically generating automated tests frowned upon?

It's pretty simple: You want to know that the function does the right thing, not just that it returns the correct type. bool isPrimeNumber(int n) { return true; } If your test says this function ...
user20574's user avatar
  • 4,907
3 votes
Accepted

How to approach writing a 'test' for a series of input->output sets, that will later be used to verify correctness of a new system?

You are trying to prepare a regression test, in order to ensure that the new implementation exhibits the existing behaviour. As a testing method, an end to end test of your component seems to be what ...
amon's user avatar
  • 135k
2 votes

How to write a unit test for a repository method that returns some data, where data can change over time?

Do you have a setCoefficients() method? If so, what I prefer to do is this. (Warning: It's controversial. My points may suffer...) Clear the TEST db. Insert data using your code. Retrieve and ...
user949300's user avatar
  • 8,919
2 votes

How to write a unit test for a repository method that returns some data, where data can change over time?

Should a unit test be written for this code at all? No, because unit tests need to run in total isolation and you test the logic within the method. Instead, write an automated test. What should I ...
Emerson Cardoso's user avatar
2 votes

Unit testing for non-stateless units - how?

You have two choices. First let’s assume that the code you are testing is not accessing the database directly, but there is some class in between. And let’s assume there is one place in your code that ...
gnasher729's user avatar
  • 46.7k
2 votes

Unit tests involving library functions and objects

NO. By the same reasoning, you could argue that no unit tests are ever needed since the whole PHP code was written by very experienced and talented people, which probably tested everything themselves....
Steve Chamaillard's user avatar
1 vote

Is bad idea to utilize helper functions on integration tests?

I think your supervisor is off track, and I think creating helper functions for integration tests is a perfect way to use OO and DRY principles in your test case design. I've made a career of ...
Todd Bradley's user avatar
1 vote
Accepted

Initialising variable within setUp() method or actual test method when changes are necessary?

The test setup is used to create the common initial state of all the unit tests in the current scope (in this case, a class). The data in the variables can change as part of the test. If you find ...
Greg Burghardt's user avatar
1 vote
Accepted

Testing an external API that's in beta state

I'd like to write test to ensure my code is working. Unit tests are already done for my own code. But the API itself isn't tested, because it would violate the first rule of FIRST (Fast tests). If ...
Thanos Tintinidis's user avatar
1 vote

Testing an external API that's in beta state

Honestly, the API you're calling should have a versioning scheme. IE, an API i've worked with in the past was formatted http://clientaddress.com/v1/endpoints, but they were in testing with a v2 that ...
Adam Wells's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible