davorminchorov's avatar

Should I use all types of tests with Codeception?

Hey,

I recently started learning more about testing and I noticed that with Codeception you can write 3 types of tests: Unit, Acceptance and Functional.

My question is: When building an application with Laravel, should I use all 3 types of tests or choose 1 of them?

0 likes
8 replies
Ramon's avatar

this answer has many variations as the different suites. Getting some quotes from stackoverflow, we can resume like this:

"Unit Tests

Tests the smallest unit of functionality, typically a method/function. Unit tests should be focussed on one particular feature (e.g., calling the pop method when the stack is empty should throw an InvalidOperationException).

Functional Tests

Functional tests check a particular feature for correctness by comparing the results for a given input against the specification. Functional tests don't concern themselves with intermediate results or side-effects, just the result (they don't care that after doing x, object y has state z). They are written to test part of the specification such as, "calling function Square(x) with the argument of 2 returns 4".

Acceptance Tests

Acceptance testing seems to be split into two types:

Standard acceptance testing involves performing tests on the full system (e.g. using your web page via a web browser [Selenium Module]) to see whether the application's functionality satisfies the specification. E.g. "clicking a zoom icon should enlarge the document view by 25%." There is no real continuum of results, just a pass or fail outcome.

The advantage is that the tests are described in plain English and ensures the software, as a whole, is feature complete. The disadvantage is that you've moved another level up the testing pyramid. Acceptance tests touch mountains of code, so tracking down a failure can be tricky.

Also, in agile software development, user acceptance testing involves creating tests to mirror the user stories created by/for the software's customer during development. If the tests pass, it means the software should meet the customer's requirements and the stories can be considered complete. An acceptance test suite is basically an executable specification written in a domain specific language that describes the tests in the language used by the users of the system."

2 likes
SetKyarWaLar's avatar

I think it's your option. If you just want to test on Unit Tests so go on with Unit Test. And if you want to test both Unit tests and Acceptance tests on Codeception it's also ok?

grandadevans's avatar

Sorry but I haven't read the 2 previous comments as yet but thought I'd add my own two penneth first.

I am on my first test first project and I have decided to implement everything through codeception.

  • The unit tests can just be run in phpstorm if wish by setting the directory in the relevant directory in the phpunit.xml file
  • I like to run codecept run --coverage --coverage-html --coverage-xml every time I have a file fully tested.

A point of note. Make sure that you are using codeception version 2.0.2 as outlined in this page (my reply with reasoning is on page 2)

psmail's avatar

My understanding is that you can't call the Laravel helper function if you use Codeception to run unit tests.

Be nice to know if I'm mistaken as is rather run all my tests from Codeception.

1 like
grandadevans's avatar

I've not used them but if you mean the ones provided by TestCase could you just copy the TestCase.php file to codeception/unit/ and then once again have your unit test extend TestCase again rather than PHPUnit_Framework_TestCase (excuse if last Class name was incorrect)?

1 like
Ramon's avatar

If you generate the phpunit' suite, you cannot use the laravel module, but with the test suite (generate:test) provides to you access to the codeception hooks like DB, L4…

davorminchorov's avatar

So creating separate tests for different parts of the application is not the best idea? If I test stuff with Functional tests, do I need unit tests or acceptance tests? Also is using multiple types of tests a good idea in general?

Ramon's avatar

I don't know your project, but a well tested application uses different tools and tests.

Unit test looks only for functions / methods (Example: Testing a function to add Ms. the beginning of a name, or convert/modify a string). Looking at a micro scenario.

After that, in a more global level, we can use a functional test, which encompasses small methods in a macro context. (Example: Testing a registration form with several fields and several small methods, as stated above).

Also you can use an Integration test, if you want to test with something like Selenium or to test JS dependencies, etc.

Jeff himself has a course on the tuts+ about Codeception, is quite interesting. Link: https://code.tutsplus.com/courses/modern-testing-in-php-with-codeception

Please or to participate in this conversation.