Take in console input for PHPUnit
I want to be prompted for an input when I run a P开发者_StackOverflow社区HPUnit test case and then to use that input as part of the test. In other words, I want to use a dynamic input value for a field.
How can I do this?
Short Answer: You shoudn't
Long Asnwer: You really shoudn't because it massively violates the goals of unittesting
While it's easy to do echo "Input for Test 7: "; $input = fgets(STDIN);
it isn't how unittesting works.
Depending on your usecase there might be a reason to do so but i can't think of any and since the solution is trivial i use the rest of the post describing why you might not want to do this.
Unittests are meant to be run often. Really really often. Many people use a setup that runs the Tests everytime you edit a file. But at least everytime before you check code into the Sourcecode management system. Do you really want to write 1 (or 10 or 50) lines everytime just so your testsuite runs ?
Also the biggest benefit of Unittests is that you have a system in place that takes care of one very important question "if i put in the same thing here as yesterday, does it still provide the same output / does it still work". So i don't see a point in putting in something different every time.
If you go one step further and use an automated system to run your tests and send an email in case the tests fail there isn't anyone there to provide input so you couldn't use that.. and one big reason to use unittesting is excatly that ;)
If you are really sure about what you doing: Sorry for the 'rant'
Edit (your reply only showed up after posting for me): The same thing sort of goes for other dynamic input. Imho you want to make sure the result is reproducable. If you just need a temporary variable/foldername/key/something thats not a big problem. I'd just use md5(mt_rand())
or something. Even so it's nice if you can set up your tests in a way so that isn't necessary
精彩评论