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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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) {
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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....
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
phpunit × 28unit-testing × 23
php × 16
testing × 8
mocking × 4
tdd × 3
integration-tests × 2
laravel × 2
code-generation × 2
design × 1
database × 1
api × 1
orm × 1
zend-framework × 1
trait × 1