Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filesystem/directory layout for unit and functional tests with PHPUnit

I have a need to add integration tests to my test suite. Although the name is PHPUnit, I find that there's plenty of support for running non-unit tests here. My layout is something like this:

<root>
    lib/
        Foo/
            Component.php
    tests/
        lib/
            Foo/
                ComponentTest.php
        resources/
            fixtures/

There are (as I see it), three ways to add higher-than-unit level tests here:

Adding a parallel structure

<root>
    tests/
        unit/
            lib/
                Foo/
                    ComponentTest.php
        integration/
            lib/
                Foo/
                    ComponentTest.php
        resources/
            fixtures/

This should work, except now there isn't a 1:1 class : test class relationship which might (and probably will) confuse Netbeans.

Adding <root>/tests/lib/Foo/ComponentIntegrationTest.php

Has the same problem as above, with less directories.

Adding the tests to the existing test class, with notation like @group integration

This keeps the 1:1 relationship, but adds significant complexity to the test suite. Also, as the SUT will need to be invoked in changing environment, I can't just reuse the _setup() from unit tests.

Ideas?

like image 652
Dalibor Karlović Avatar asked Oct 31 '11 12:10

Dalibor Karlović


1 Answers

Unit tests are used to test a single unit (class) in isolation. Integration tests are designed to test multiple units together. As such you probably won't have a 1:1 correlation between classes and the integration tests to which they belong.

I would create a separate directory structure--perhaps even a separate project--to house the integration tests. NetBeans won't be able to open "the test" that a class belongs to because it will belong to multiple tests, but it will continue to open its matching unit test.

We have a separate project for our integration tests (called sanity tests before I arrived) using Selenium, and it works well enough so far.

Update

The directory layout for our integration tests is pretty simple: one directory per site. We have a single codebase for the site and use views to allow single sites to override the layout (more than just skinning). We haven't spent as much time on these tests as I'd like. We use Selenium to run the tests after pushing the sites to a staging server. I haven't had a chance to hook them up to our continuous integration server (Jenkins) yet, but that would be ideal.

I'd recommend organizing them by functional area or module on your site. Mostly, it depends on how you think about the project you're testing. An example might look like this:

src/
    Model/
        Cart.php
    ...
tests/
    unit/
        phpunit.xml
        bootstrap.php
        Model/
            CartTest.php
        ...
    integration/
        account/
            login/
            register/
            subscriptions/
        products/
            listing/
            details/
            search/
        cart/
            shop/
            checkout/
like image 62
David Harkness Avatar answered Nov 19 '22 21:11

David Harkness