I have a application developed by laravel 5.3 . Before deploying script i would like test unit test my whole application build using php unit .. Is any package or addons available to test my application build.Else how could i use the unit testing in Laravel 5.3 ..
I'm new to this .. Please any one help
Laravel comes with a TestCase class which you should extend and write your own tests from:
class FooTest extends TestCase {
public function testSomethingIsTrue()
{
$this->assertTrue(true);
}
}
From the Laravel Docs:
You may then define test methods as you normally would using PHPUnit. To run your tests, simply execute the phpunit command from your terminal
Related
I usually use GitLab to host my repositories and to use their CI/CD.
I made a Symfony project (PHP) with some code. I know how to configure my .gitlab-ci.yml to execute my unit tests through PHPUnit.
But I don't really understand how to execute my functional test. For example this code is just testing that the route /login is reachable and not return a 500.
class SecurityControllerTest extends WebTestCase
{
public function testLogin()
{
$client = static::createClient();
$client->request('GET', '/login');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}
So I assume that apache/nginx is needed to interpret PHP. So do I have to make a real docker-compose with a complete LAMP stack or is there a better way?
Functional tests are executed directly in phpunit. You only need to install browser-kit and dom-crawler symfony components.
If you use an ORM, you have to declare a database image as a service and initialize your database in your CI script.
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.
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
Using Symfony\Bundle\FrameworkBundle\Test\WebTestCase, we get easy access to container, entity manager etc. We can use it for functional testing by automatic manual HTTP requests.
Can we use it to test Symfony2 console commands as well so that we can have easy access to container and all services?
I want to test my costum Symfony2 console commands which uses many services which in turn uses doctrine entity manager to access data.
PHPunit documentation suggest to extend the test class with PHPUnit_Extensions_Database_TestCase,
Can we extend WebTestCase instead of test instead to test console commands ?
I have already refereed
How to test Doctrine Repositories Cookbook
How to test code that interacts with the Database Cookbook
The Console Component Docs
WebTestCase is meant for functional testing your web applications. Nothing will stop you from using it to test commands, but it doesn't feel right (hence your question).
Testing commands
Remember, that command tests (as well as controller tests) shouldn't be complex, just like the code you're putting in there shouldn't be complex either.
Treat your commands as controllers, make them slim and put your business logic where it belongs - to the model.
Accessing the container
Having said that, you can implement your own KernelAwareTestCase (or ContainerAwareTestCase) yourself. Here's a base class I'm using occasionally: jakzal / KernelAwareTest.php Gist
Also, note that next to Symfony\Component\Console\Application there's a Symfony\Bundle\FrameworkBundle\Console\Application which can actually work with the Symfony kernel.
Final note
Remember, that the most extensive testing should be done on a unit level.
I'm just getting started with unit testing in the zend framework. I've read lots of the docs ZF and PhpUnit but there are a few things I can't figure out.
I have Zend Framework set up. Do I need to install PHPUnit too or is it all sorted within the framework?
Zend Framework has created files for the unit tests under /tests/application/controllers/ControllerNameTest.php. I assume I create my tests here.
How do I run a test? I'm sure this is really simple because the docs I have read assume I should know how to do this. Do I do it from the command line? If so, how?
Any help appreciated.
Thanks
You need to install PHPUnit, too. Zend Framework and PHPUnit are two different things. You find the installation instructions for PHPUnit here: https://github.com/sebastianbergmann/phpunit.
Basically, You can put the tests where you want. But the tests-Folder is a good place for them.
After you have installed phpunit, you can call your unit tests from the command line. Just enter the folder where you put your tests on command line and type "phpunit", this will run all tests in the folder. You can also use the --filter option to run a single test.
You need to install the phpunit. The easiest way in my opinion is using PEAR (http://www.phpunit.de/manual/current/en/installation.html#installation.pear).
For testing controllers you can replicate the same code which you found in the ControllerNameTest.php file. For testing models you can create a PHPUnit test case.
<?php
class YourApp_Model_YourModel extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap('config')
->bootstrap('defaultModuleAutoloader')
->bootstrap('autoloader');
// You might need to add few more bootstrap, depends on your needs
}
public function testSomeMethod(){}
I hope that helps.
I have found a great guide to setting everything up here with PHPUnit and Zend Framework here:
http://grover.open2space.com/content/unit-testing-zend-framework-111-and-phpunit