I was writing unit tests for half year or more at my work, hoping that I do good job. But the problem is I rarely see the benefit of them. So I don't understand, why it is good. Many of the test functions look like this - they test model functions.

    protected function test_get_dogs_8_subtypes() {

        $test_result = $this->Dogs_8_model->get_dogs_8_subtypes();

        $expected_result = 'is_array';
        $test_name = __FUNCTION__;
        $this->unit->run($test_result, $expected_result, $test_name);

    }

I run, if there is errors they fail so ok. But they will fail when I modify them. If I don't then they will never fail.

But when I modify some model function, I test the functionality of the sofware by hand anyway. So if there is error, functionality does not work and I fix the model function. So what extra thing this unit test for me does?

There were cases when unit testing made benefit. But that was like 5% of the cases probably. For example I was writing class which finds what is poker combination - flush, or straight or 2 pair, etc.

There those functions were depending heavily on each other. I don't remember exacluy, but checking if there is full house combination, I used to check if there is pair and trips combination. Do if I edit trips checking function then fullhouse checking function as well might be broken.
There the unit test helped a lot ,because when editing one function there was high probability of other functions braking and unit test quickly found out those.

But I don't see this with model functions.

Why is that? Maybe I don't get somehting? Maybe I should stop making those unit tests because they cost time, which means money, and giving no benefit.

Personally, I agree. If the method is so obvious, skip the test. Focus your attention on the complex ones.

Can you explain what the above method is testing? Just to see if an array is returned? Shouldn't it test inserting specific data and test whether that is returned correctly?

Unit Tests are primarily written as a good practice to help developers identify and fix bugs, to refactor code and to serve as documentation for a unit of software under test. To achieve these benefits, unit tests ideally should cover all the possible paths in a program. One unit test usually covers one specific path in one function or method. However a test method is not necessary an encapsulated, independent entity. Often there are implicit dependencies between test methods, hidden in the implementation scenario of a test.

http://www.phpunit.de/manual/3.6/en/writing-tests-for-phpunit.html

Can you explain what the above method is testing? Just to see if an array is returned? Shouldn't it test inserting specific data and test whether that is returned correctly?

yeah, its testing if its array. I cannot know which data exacly it will return, because the data is fro database and in database there can be any data.

I could test which array keys are returned, count of keys. I did that at first, but noitced that it will take ages to do this stuff and might be no benefit.

And most model fucnitons are like this - returns some array. So don't test those?

Then were to use unit testing in PHP practically? I read unit testing is good, but can find only few % of code where its good. I cannot understand why is that.

I could be testing javascript, user interface, but I am not sure yet how is UI tested, so not doing it. MAybe later some time will start.

@IIM
your example tests function where there is some bit different opeartion done and is known exact result. Like if I would be writin function array_push() then I completely understand this test - it tests is this function working with specific input and gets specific output.

But with database - we have input - but we cannot know the output. We only can kwow some part of the outputy - for example what is datatype. Unless we make an isert into databse before,then retirve - so we know htat by retireving - wr should get the data which we inserted. BUt its lot of time to do that - and no practical benefit almost.

And most model fucnitons are like this - returns some array. So don't test those?

A usual model test starts with inserting a specific set of data (even trying to insert faulty data to see it doesn't get inserted), then tests if it is returned correctly, then removes the data. If it just test to see if an array is returned, the test is pointless.

hmm, so if there is model fucntion joining like 10 tables, I will need to insert into all tables, some tables even has to have more rows if simulate real data, the amounts of used time for making tests will be huge.

Are you yourself doing it?

Yes. Not in PHP though, but you are right, making the tests takes a lot more time than the actual code. The point is, that when you add a NOT NULL column for example, the model test instantly fails if the model hasn't been updated. Just testing to see if you get an array, will still pass.

Yes Right! Just First please focus on development at this time just skip the testing work.

Which type of of Testing you did till now?

I aggree that those tests which test exact data will fail if nulls and so on.

Yes. Not in PHP though
Why not in PHP?

Yes Right! Just First please focus on development at this time just skip the testing work. Which type of of Testing you did till now?

TDD is made for actually making tests first and then development. And people recoment TDD, when I read some articles.
Of course I myself want to avoind bugs in the software. And I don't know which type of testing it is. You can see how the test function looks in my first post.

I was not testing controllers, because I did not found a way to call a controller function from controller (at least in Codeigniter) and I did not want to have unit test functions in same file. But now thinking that maybe I could do this way: make a unit test class which extends controller class, something like that.

Why not in PHP? Because it's not the language I'm currrently programming in for work.

Try to make a unit test class which can help you!

David Kroj

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.