-2

I'm setting up a test suite with Codeception. I need both my app and Codeception to connect to the same test database, but for my app to connect to the dev database when it's not being tested.

I'm looking for some way to tell my app that it's being tested so that it can then connect to the right database.

I'm assuming there's some way to set an environmental variable within my Codeception test that my app can then read, but I've Googled everything I can think of, but I just end up going in circles. I feel like I'm missing something obvious.

user3356802
  • 141
  • 2
  • 8
  • 1
    create a .env.testing file in your projects root directory with the DB variables for your test database. – Alec Joy Oct 30 '19 at 16:04
  • I have that. What's not clear to me is how my app knows whether to use .env or .env.testing. – user3356802 Oct 30 '19 at 16:07
  • Codeception wraps PHPUnit, in your phpunit.xml file in your project root their should be a line called either `` or `` depending on your laravel version. That sets your Laravel app to the testing .env when running your tests, and laravel looks for a .env.testing file to setup that environment. – Alec Joy Oct 30 '19 at 16:15
  • I'm afraid I'm not using Laravel. There's no phpunit.xml file in my root or in any Codeception folders, and other answers about Codeception suggest that it doesn't read it. Creating one seems to have no effect. – user3356802 Oct 30 '19 at 16:26
  • @user3356802 What Codeception module do you use for testing your app? – Naktibalda Oct 31 '19 at 07:59
  • @Naktibalda The only modules I'm using at the moment are WebDriver and Db. – user3356802 Oct 31 '19 at 09:51

1 Answers1

1

WebDriver module of Codeception interacts with application over HTTP and can't affect its configuration in any way.

The only way you can tell application that it is being tested is by setting some cookie using setCookie and checking it in application code. But I wouldn't risk having such code in production.

A much better option is to deploy application to test server/environment/etc and configure it accordingly.

Naktibalda
  • 13,705
  • 5
  • 35
  • 51