2

I have written unit tests with phpunit for a RESTful API.

Now I want to use phpunit for further integration tests. Since these are much slower I built a special testsuite "integration" which has to be called explicitly. So unit tests can run often and stay fast.

The bootstrap.php of the integration testsuite performs a database dump and seeding. The integration tests are quite slow so they will not be called as often as the unit tests. They will only be run by humans.

To prevent an unintended deletion of the local development database I want the testscript to prompt for confirmation before dumping the database. Only if confirmed the database will be deleted and re-seeded.

  1. Is it possible to prompt for input in phpunit tests?
  2. Does a prompt hurt any "design principle of integration tests"?

Thank you.

3
  • you could use the $argv values to validate before dumping. php.net/manual/en/reserved.variables.argv.php
    – D_R
    Mar 16, 2015 at 16:36
  • 2
    A better approach here, in my opinion, is to set up database environments so that your tests don't touch the development database at all. Use a separate one, and then it doesn't matter if the data is rebuilt - it won't touch what you are doing in dev. I'd take the view that tests need to be automated - if you run integration tests remotely (e.g. on a CI server) how would you accept user input in that case?
    – halfer
    Mar 16, 2015 at 16:48
  • 1
    Thank you for replies! I will try to set up a integration test environment.
    – sgotre
    Mar 16, 2015 at 16:56

1 Answer 1

3

To prevent an unintended deletion of the local development database I want the testscript to prompt for confirmation before dumping the database. Only if confirmed the database will be deleted and re-seeded.

A basic prompt could look like this:

<?php

echo "Are you sure you want to drop the database?\n";
echo "Type 'yes' or 'y' to continue: ";

$handle = fopen("php://stdin","r"); // read from STDIN
$line = trim(fgets($handle));

if($line !== 'yes' && $line !== 'y'){
    echo "Execution stop.\n";
    exit;
}

echo "\n Continuing execution: dropping database.\n";

Is it possible to prompt for input in phpunit tests?

Yes, it's possible - but beware a possible timeout.

Does a prompt hurt any "design principle of integration tests"?

A prompt for user data ("data entry prompt") during your tests would indicate a real bad automation state. It's an indicator that your tests are not prepared to run fully automated.

This prompt is "accident protection" during the test setup or bootstrap phase and there is no need for it in a boxed or isolated test environment.

It's better to strive for full test automation and use a Continuous Integration server for running your functional and integration tests. (Just like @halfer pointed out before.)

Keep in mind, that you can also detect the environment your tests run in. Simply skip the prompt, when you are on the CI server.

Well, there a certain "testing patterns" or "best practices".

  • Like (sub)grouping your TestSuites, in order to speed it up with parallel execution.
  • Avoid "will only be run by humans" test executions! Better: "runs fully automated" and/or "runs automated in 1 out of 10 test runs".
  • A data prompt in a test is a "Test Run Blocker".
  • A prompt based on the env or a constant and fired during test bootstrap is ok.
2
  • Thank you for this great answer. I was not sure about the right "testing patterns" and especially the hint to avoid "run by humans only" made clear my question was a indicator for "bad design" :)
    – sgotre
    Mar 17, 2015 at 13:06
  • 2
    just one small typo: you should use && instead of || here: if($line !== 'yes' && $line !== 'y')
    – sgotre
    Mar 17, 2015 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.