You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 2 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Running Application Tests
  2. This is the quick-start to CodeIgniter testing. Its intent is to describe what
  3. it takes to set up your application and get it ready to run unit tests.
  4. It is not intended to be a full description of the test features that you can
  5. use to test your application. Those details can be found in the documentation.
  6. ## Resources
  7. * [CodeIgniter 4 User Guide on Testing](https://codeigniter4.github.io/userguide/testing/index.html)
  8. * [PHPUnit docs](https://phpunit.de/documentation.html)
  9. ## Requirements
  10. It is recommended to use the latest version of PHPUnit. At the time of this
  11. writing we are running version 9.x. Support for this has been built into the
  12. **composer.json** file that ships with CodeIgniter and can easily be installed
  13. via [Composer](https://getcomposer.org/) if you don't already have it installed globally.
  14. > composer install
  15. If running under OS X or Linux, you can create a symbolic link to make running tests a touch nicer.
  16. > ln -s ./vendor/bin/phpunit ./phpunit
  17. You also need to install [XDebug](https://xdebug.org/index.php) in order
  18. for code coverage to be calculated successfully.
  19. ## Setting Up
  20. A number of the tests use a running database.
  21. In order to set up the database edit the details for the `tests` group in
  22. **app/Config/Database.php** or **phpunit.xml**. Make sure that you provide a database engine
  23. that is currently running on your machine. More details on a test database setup are in the
  24. [Testing Your Database](https://codeigniter4.github.io/userguide/testing/database.html) section of the documentation.
  25. If you want to run the tests without using live database you can
  26. exclude @DatabaseLive group. Or make a copy of **phpunit.dist.xml** -
  27. call it **phpunit.xml** - and comment out the <testsuite> named "database". This will make
  28. the tests run quite a bit faster.
  29. ## Running the tests
  30. The entire test suite can be run by simply typing one command-line command from the main directory.
  31. > ./phpunit
  32. If you are using Windows, use the following command.
  33. > vendor\bin\phpunit
  34. You can limit tests to those within a single test directory by specifying the
  35. directory name after phpunit.
  36. > ./phpunit app/Models
  37. ## Generating Code Coverage
  38. To generate coverage information, including HTML reports you can view in your browser,
  39. you can use the following command:
  40. > ./phpunit --colors --coverage-text=tests/coverage.txt --coverage-html=tests/coverage/ -d memory_limit=1024m
  41. This runs all of the tests again collecting information about how many lines,
  42. functions, and files are tested. It also reports the percentage of the code that is covered by tests.
  43. It is collected in two formats: a simple text file that provides an overview as well
  44. as a comprehensive collection of HTML files that show the status of every line of code in the project.
  45. The text file can be found at **tests/coverage.txt**.
  46. The HTML files can be viewed by opening **tests/coverage/index.html** in your favorite browser.
  47. ## PHPUnit XML Configuration
  48. The repository has a ``phpunit.xml.dist`` file in the project root that's used for
  49. PHPUnit configuration. This is used to provide a default configuration if you
  50. do not have your own configuration file in the project root.
  51. The normal practice would be to copy ``phpunit.xml.dist`` to ``phpunit.xml``
  52. (which is git ignored), and to tailor it as you see fit.
  53. For instance, you might wish to exclude database tests, or automatically generate
  54. HTML code coverage reports.
  55. ## Test Cases
  56. Every test needs a *test case*, or class that your tests extend. CodeIgniter 4
  57. provides a few that you may use directly:
  58. * `CodeIgniter\Test\CIUnitTestCase` - for basic tests with no other service needs
  59. * `CodeIgniter\Test\DatabaseTestTrait` - for tests that need database access
  60. Most of the time you will want to write your own test cases to hold functions and services
  61. common to your test suites.
  62. ## Creating Tests
  63. All tests go in the **tests/** directory. Each test file is a class that extends a
  64. **Test Case** (see above) and contains methods for the individual tests. These method
  65. names must start with the word "test" and should have descriptive names for precisely what
  66. they are testing:
  67. `testUserCanModifyFile()` `testOutputColorMatchesInput()` `testIsLoggedInFailsWithInvalidUser()`
  68. Writing tests is an art, and there are many resources available to help learn how.
  69. Review the links above and always pay attention to your code coverage.
  70. ### Database Tests
  71. Tests can include migrating, seeding, and testing against a mock or live<sup>1</sup> database.
  72. Be sure to modify the test case (or create your own) to point to your seed and migrations
  73. and include any additional steps to be run before tests in the `setUp()` method.
  74. <sup>1</sup> Note: If you are using database tests that require a live database connection
  75. you will need to rename **phpunit.xml.dist** to **phpunit.xml**, uncomment the database
  76. configuration lines and add your connection details. Prevent **phpunit.xml** from being
  77. tracked in your repo by adding it to **.gitignore**.