Run phpunit tests by testcase class that they extend - php

I want to separate my tests in unit and integration tests and have an abstract TestCase class for each, e.g. UnitTestCase and IntegrationTestCase.
Is there a nice way to run only those tests that extend UnitTestCase without giving a #group unit annotation to each of these test classes?
The phpunit documentation is very sparse when describing subclassing. Also a google search didn't turn up any useful results.

No. Either use the #group annotation for this or, better IMO, have a tests/unit directory for unit tests as well as a tests/integration directory for integration tests and then define two test suites in phpunit.xml. By default, both will be run. Using --testsuite you can then filter based on the test suite name.

Related

PHPUnit Code Coverage False Positives?

I ran phpunit with code coverage in order to see which ones of my classes are less tested.
I arbitrarily excluded some folders such as DataFixtures (Doctrine fixtures) and Admin (Sonata Admin classes) and this is the result:
I noticed that the Entity folder is all in red but it contains only classes with a lot of standard getters and setters. Same thing for the Model folder.
How can I test them without ignoring?
PHPUnit offers some ways to modify how coverage is tracked using annotations on yor tests, such as #covers and #coversNothing. You can also exclude sections of your code from coverage by using #codeCoverageIgnore. How to use them is covered in the docs: https://phpunit.de/manual/current/en/code-coverage-analysis.html
When running the tests you can use command line options to modify some of those settings:
--strict-coverage Be strict about #covers annotation usage.
--disable-coverage-ignore Disable annotations for ignoring code coverage.
If your problem does not stem from limiting coverage on those entities, you should identify which tests use them to see if the methods are actually called. I could imagine that you have mocked them and that is why they are not covered.

How to codeCoverageIgnore for specific test suite only in phpunit

I have defined two test-suites in phpunit.xml file, one for Unit Tests and other for Functional Test. For some functions which can't be unit tested, I am using #codeCoverageIgnore in my source code. But I do not want this code to be ignored when running Functional Tests.
Is it possible to add test-suite name to codeCoverageIgnore so that my code is ignored for unit tests and not for functional tests.
No, that is not possible. If you think it should be possible then please open a ticket.

Use \PHPUnit_Framework_TestCase or ..\TestCase

Stupid question... In the Symfony docs, unit test classes extend \PHPUnit_Framework_TestCase but in PHPUnit docs, test classes extend PHPUnit\Framework\TestCase. Is there a difference, are they the same, or am I missing a subtle PHP feature where _ is equivalent to \?
Short of actually testing it out myself, can I use either, or just one in Symfony?
Update: It's been helpfully explained to me that these two classes are just a difference between PHPUnit versions. After my own research and for the record, Symfony 3.1 still extends \PHPUnit_Framework_TestCase in its own KernelTestCase class that powers Symfony's functional testing. To avoid breaking code, I'm going to play safe & extend WebTestCase that inherits from \PHPUnit_Framework_TestCase when unit testing with Symfony specifically. Such unit tests still work with PHPUnit 5.6 anyway
These are the same but since PHPUnit 5.4 it's recommended to use TestCase. However, it's not a problem if you stay with PHPUnit_Framework_TestCase.
See a very similar question: PHPUnit can't found the "TestCase" class

Specify a Group of PHPUnit Tests to Run in Seperate Processes

In PHPUnit, you can annotate a test-case class (#runTestsInSeparateProcesses) or a test function (#runInSeparateProcess) to run tests in process isolation. There is also a setting in the phpunit.xml config file (processIsolation = "true") to run all tests in separate processes.
I have a group of test-case classes which all inherit from a database-related abstract test class that I need to run in process isolation. But annotations aren't inherited from parent classes. Is there a way for me to mark these test-case classes to run in separate processes by #group, or directory, or some other specifier, without having to mark each test-case class with #runTestsInSeparateProcesses?
No, this is currently not possible. Feel free to open a feature request.

How to trouble shoot codeception unit tests in laravel 4

While my other Codeception tests are running (acceptance, api, etc,), very little of the unit tests will run. It does not see any tests of type Test.php, only Cept.php and Cest.php. It does not accept (errors) any assertion tests such as "$this->assertEquals($expeced. 'expect this'). How do I trouble shoot this as the tests that fail only return:
Codeception PHP Testing Framework v1.8.1
Powered by PHPUnit 3.7.28 by Sebastian Bergmann.
phpunit seems to be installed as I see it in the vendor directory. I can run it directly from the command line to see its help. But that is about it. Are unit tests generally flaky with the Laravel/Codeception combo? Is there some config I am missing???
Seems like the problem was the creation of the test class.
You declared you want to use \CodeGuy; - you don't want to do that.
Instead you want to extend your class like this.
class SampleCest extends \Codeception\TestCase\Test
My advise: use codeception to generate your unit tests.
codecept.phar generate:phpunit unit Sample

Categories