Practices for database testing in Symfony2? How to isolate?

前端 未结 4 1883
别那么骄傲
别那么骄傲 2021-02-13 19:42

What are the current best practices for testing database interaction with Symfony2? I have a simple CRUD setup and i want to make sure my testing is OK. Right now, i have 4 test

4条回答
  •  醉酒成梦
    2021-02-13 20:15

    Database testing is always slow as you need to create/drop your schema before/after each test. To avoid unnecessary operations, you could:

    • create schema in the 'setUpBeforeClass' method;
    • ensure your 4 tests are launched in one thread with the '@depend' annotation;
    • drop schema in the 'tearDownAfterClass' method.

    The schema will be created/droped only once for your tests case.

    You can also setup doctrine to use an inmemory sqlite database (which is very fast):

    doctrine:
        dbal:
            driver: pdo_sqlite
            path: :memory:
            memory: true
    

    Anyway, '_construct' and '_destruct' should never be used in phpunit test cases, instead you should use 'setUp' and 'tearDown'.

提交回复
热议问题