What are some strategies that could be used to automate the creation of unit test cases? What aspects would you need to look at in each class to be able to generate at least a decent test case skeleton? I realize a comprehensive automatic solution isn't practical, but I'd like to speed up the test creation a little at least by creating a skeleton. I'm not looking for code examples, just perhaps some suggestions for where to start or examples of where something like this has been done so that I can see how they approached it and what might be possible.I'm particularly interested in methods for creating unit test skeletons in PHP, which doesn't provide all of the tools that other languages afford, like complete type hinting, for example.
Automating Unit Test Creation
php;unit testing
Your strategy and skeleton depends, non-trivially, on what kinds of tests you're looking to generate, what kind of coverage you're looking for, and the language/environment you're working in.It's fairly straight forward to write a test generator which, for languages like C or Java, reads class signatures and automatically generates tests for standard corner cases (passing in 0, 2 random values, MAX_INT, MIN_INT, to an integer argument, nulls for nullables, etc...). You could then run the generated tests, record the results for each test, and manually filter through them to remove irrelevant ones, approve acceptable results for tests which pass (so they can automatically pass from then on), and mark as invalid ones which fail.You can augment this with tagging/commenting/refactoring of classes to help your generator with extra hints. You might have a tag which lists all the possible exceptions that a method call is allowed to raise, or which gives a reduced range of valid integers for an integer argument. Look at these as short-hand for having to write the tests yourself.So, here are some components you'll want to look at:A component to automatically parse source code/function signatures/manual annotations, producing standard test cases, or outlines/signatures for test cases which wait for your input to be completed.A perpetually growing/changing language of tags/annotations/comments which may go to any level of granularity (method/class/signature/while loops/etc...) representing hints to the automated test builder. Ideally you should be able to play with this language without having to recode your framework or any chunks in itAutomated test runner, with the ability to identify new/old tests and record/test against acceptable answers for each test. Ideally this runner will build a database of test runs, results accepted/declined, and current acceptable results for each test.Automated object faker which, given a class name and map of names->values, can generate an object mimicking the class, returning customizable data for function calls, accessors, public data slots, etc...There are lots of test frameworks out there which already include chunks of this functionality for various languages and platforms. While it's fairly easy to start doing this work yourself and grow this kind of framework organically in-house, it's also an endless long-term project which will probably be duplicating existing work. I'd recommend taking significant time to look at what's available first, and then decide whether it's worth the time to dive in.