0

PhpUnit has a generator of skel based on an existing class.

But it's works once.

If later some new method are added (because a dev doesnt work with tdd ), the test file is incomplete.

Is there a tool to generate a skel for uncovered method ?

Moosh
  • 386
  • 1
  • 8

2 Answers2

0

I don't know any, and I also don't see the need. That skeleton generator generates one test method per function it finds, but you cannot test all use cases of a slightly advanced function within only one test function.

Also, the name of the test function is generated - but better names can and should be created to describe the intended test case or behavior of the tested function. Like "testGetQuoteFromStockMarket" vs. "testGettingMicrosoftQuoteFromStockMarketShouldReturnQuoteObject" and "testGettingUmbrellaCorporationFromStockMarketShouldFailWithException".

Note that you cannot test the throwing of exceptions in combination with cases that do not throw exceptions.

So all in all there simply is no use case to create "one test method per method" at all, and if you add new methods, it is your task to manually add the appropriate number of new tests for that - the generated code coverage statistics will tell you how well you did, or which functions are untested.

Sven
  • 69,403
  • 10
  • 107
  • 109
  • Imagine a class with one method foo() , I run generator I's build a test file with skel testFoo(). Later I add in same class a method bar() I just wan run the generator wich add (append) skel testBar() in the same test file than testFoo() – Moosh Jan 26 '14 at 23:36
  • Imagine that class with one method `foo()`. To test this method completely (because it throws an exception if used with a wrong parameter) you have to create two test functions: `testFooShouldWork()` and `fooShouldThrowException()`. If you later add a new method `bar()` to the class, you initially should add a test method to the test (no need for the skeleton generator), or you should be able to add that simple test method after you wrote your code. What should the generator do in this case: Create another function `testFoo()` because the previous got renamed, and you then remove it? – Sven Jan 27 '14 at 16:58
0

AFAIK there is no built-in phpunit functionality to updated the auto-generated test code; that is typical of most code generators.

The good news is that each of the functions is added quite cleanly and independently. So what I would do is rename your existing unit test file to *.old, regenerate a fresh test file, then use meld (or the visual diff tool of your choice) to merge in the new functions.

Aside: automatic test generation is really only needed at the start of a new class anyway; the idea of exactly one unit test per function is more about generating nice coverage stats to please your boss; from the point of view of building good software, some functions will need multiple tests, and some functions (getters and setters come to mind) do not really need any, and sometimes multiple functions are best covered by a single unit test (getters and setters again come to mind).

Darren Cook
  • 27,837
  • 13
  • 117
  • 217
  • Yep I agree. My need is in 2 case. Unmaintained test. Lot of classes have method added without test, so I just want fill existing tests with skel of missing method (marked as skipped) and so have a better visibility on TODO My other case, I need to add feature to a class. a- I begin to write/define method signature into the class. b- run the generator c- compare generated with existing class with meld d- add new method in existing e- fill the test f- write the code to success of test. And I just want b include c & d – Moosh Jan 27 '14 at 07:52
  • @Moosh I suppose as an alternative to `meld` you could run the generator in a git branch, created just for that purpose, then auto-merge in the branch. Easy to go back if it corrupts your existing tests then. – Darren Cook Jan 27 '14 at 08:29