Drupal Behat Tests Fail: Database connection is not defined

Hi all. This is my first project on CircleCI and I haven’t been able to get it passing at all. I have a Drupal Project I’m making just to learn CI, so there’s only a single behat test that is passing locally right now. When I run it on CircleCI however I get the error:

In Database.php line 361:

The specified database connection is not defined: default

behat [-s|–suite SUITE] [-f|–format FORMAT] [-o|–out OUT] [–format-settings FORMAT-SETTINGS] [–init] [–lang LANG] [–name NAME] [–tags TAGS] [–role ROLE] [–story-syntax] [-d|–definitions DEFINITIONS] [–snippets-for [SNIPPETS-FOR]] [–snippets-type SNIPPETS-TYPE] [–append-snippets] [–no-snippets] [–strict] [–order ORDER] [–rerun] [–stop-on-failure] [–dry-run] [–] []

Exited with code 1

Here’s my config.yml:

version: 2
jobs:
  build:
    docker:
      - image: cibuilds/drupal:latest
      - image: mariadb:10.3
        environment:
          MYSQL_DATABASE: drupal
          MYSQL_ROOT_PASSWORD: HorriblePassword
          MYSQL_ROOT_HOST: "%"
    environment:
      BLUEMIX_ORG: CircleCI
      BLUEMIX_SPACE: dev
    working_directory: /project/app
    steps:
      - checkout:
          path: /project
      - run:
          name: "Setup Database & Server"
          command: |
            echo "127.0.0.1      db" >> /etc/hosts
            sleep 20
            mysql -uroot -pHorriblePassword -h127.0.0.1 drupal < ../db/drupal-db-dump.sql
            service apache2 start
      - run:
          name: "Install Dependencies"
          command: |
            composer install
      - run:
          name: "Behat Tests"
          command: |
            vendor/bin/behat --init
            vendor/bin/behat -dl
            vendor/bin/behat

Would appreciate the help. Thanks.

This looks like a general test problem that you’ve have to debug if you were experiencing it locally. Either Behat wants something on the command line (and you haven’t supplied it) or it is looking for a file (and it cannot find it).

If you want to debug this in situ, go to the detail page of a failed job, and choose the “Re-run with SSH” option. That will get you SSH access into a post-fail environment, and you can debug/rerun manually from there.

Thanks for that tip, I didn’t realize I could jump into the post build environment. It seems that my DB did get set up correctly from the .sql file import, but that the settings didn’t make their way to settings.php.

  1. Would you happen to have a link to documentation that goes into how to ensure that the settings get updated?

  2. What do you make of this error? Once I manually configured the db (still need to solve 1 above) then it gives me this error for the simple example that runs without complaint on my local machine.

    Feature: Test Features
    Some test scenarios to make sure the website is generally working.

    @api
    Scenario: Run cron # features/global.feature:5
    Given I am logged in as a user with the “administrator” role # Drupal\DrupalExtension\Context\DrupalContext::assertAuthenticatedByRole()
    When I run cron # Drupal\DrupalExtension\Context\DrupalContext::assertCron()
    And am on “admin/reports/dblog” # Drupal\DrupalExtension\Context\MinkContext::visit()
    Then I should see the link “Cron run completed” # Drupal\DrupalExtension\Context\MinkContext::assertLinkVisible()
    Exception: No link to ‘Cron run completed’ on the page http://localhost/admin/reports/dblog in vendor/drupal/drupal-extension/src/Drupal/DrupalExtension/Context/MinkContext.php:245
    Stack trace:
    #0 [internal function]: Drupal\DrupalExtension\Context\MinkContext->assertLinkVisible(‘Cron run comple…’)
    #1 vendor/behat/behat/src/Behat/Testwork/Call/Handler/RuntimeCallHandler.php(109): call_user_func_array(Array, Array)
    #2 vendor/behat/behat/src/Behat/Testwork/Call/Handler/RuntimeCallHandler.php(64): Behat\Testwork\Call\Handler\RuntimeCallHandler->executeCall(Object(Behat\Behat\Definition\Call\DefinitionCall))
    #3 vendor/behat/behat/src/Behat/Testwork/Call/CallCenter.php(140): Behat\Testwork\Call\Handler\RuntimeCallHandler->handleCall(Object(Behat\Behat\Definition\Call\DefinitionCall))
    #4 vendor/behat/behat/src/Behat/Testwork/Call/CallCenter.php(96): Behat\Testwork\Call\CallCenter->handleCall(Object(Behat\Behat\Definition\Call\DefinitionCall))
    #5 vendor/behat/behat/src/Behat/Behat/Tester/Runtime/RuntimeStepTester.php(125): Behat\Testwork\Call\CallCenter->makeCall(Object(Behat\Behat\Definition\Call\DefinitionCall))
    #6 vendor/behat/behat/src/Behat/Behat/Tester/Runtime/RuntimeStepTester.php(73): Behat\Behat\Tester\Runtime\RuntimeStepTester->testDefinition(Object(Behat\Behat\Context\Environment\InitializedContextEnvironment), Object(Behat\Gherkin\Node\FeatureNode), Object(Behat\Gherkin\Node\StepNode), Object(Behat\Behat\Definition\SearchResult), false)
    #7 vendor/behat/behat/src/Behat/Behat/Hook/Tester/HookableStepTester.php(74): Behat\Behat\Tester\Runtime\RuntimeStepTester->test(Object(Behat\Behat\Context\Environment\InitializedContextEnvironment), Object(Behat\Gherkin\Node\FeatureNode), Object(Behat\Gherkin\Node\StepNode), false)
    #8 vendor/behat/behat/src/Behat/Behat/EventDispatcher/Tester/EventDispatchingStepTester.php(73): Behat\Behat\Hook\Tester\HookableStepTester->test(Object(Behat\Behat\Context\Environment\InitializedContextEnvironment), Object(Behat\Gherkin\Node\FeatureNode), Object(Behat\Gherkin\Node\StepNode), false)
    #9 vendor/behat/behat/src/Behat/Behat/Tester/StepContainerTester.php(59): Behat\Behat\EventDispatcher\Tester\EventDispatchingStepTester->test(Object(Behat\Behat\Context\Environment\InitializedContextEnvironment), Object(Behat\Gherkin\Node\FeatureNode), Object(Behat\Gherkin\Node\StepNode), false)
    #10 vendor/behat/behat/src/Behat/Behat/Tester/Runtime/RuntimeScenarioTester.php(76): Behat\Behat\Tester\StepContainerTester->test(Object(Behat\Behat\Context\Environment\InitializedContextEnvironment), Object(Behat\Gherkin\Node\FeatureNode), Object(Behat\Gherkin\Node\ScenarioNode), false)
    #11 vendor/behat/behat/src/Behat/Behat/Hook/Tester/HookableScenarioTester.php(74): Behat\Behat\Tester\Runtime\RuntimeScenarioTester->test(Object(Behat\Behat\Context\Environment\InitializedContextEnvironment), Object(Behat\Gherkin\Node\FeatureNode), Object(Behat\Gherkin\Node\ScenarioNode), false)
    #12 vendor/behat/behat/src/Behat/Behat/EventDispatcher/Tester/EventDispatchingScenarioTester.php(103): Behat\Behat\Hook\Tester\HookableScenarioTester->test(Object(Behat\Behat\Context\Environment\InitializedContextEnvironment), Object(Behat\Gherkin\Node\FeatureNode), Object(Behat\Gherkin\Node\ScenarioNode), false)
    #13 vendor/behat/behat/src/Behat/Behat/Tester/Runtime/IsolatingScenarioTester.php(69): Behat\Behat\EventDispatcher\Tester\EventDispatchingScenarioTester->test(Object(Behat\Behat\Context\Environment\InitializedContextEnvironment), Object(Behat\Gherkin\Node\FeatureNode), Object(Behat\Gherkin\Node\ScenarioNode), false)
    #14 vendor/behat/behat/src/Behat/Behat/Tester/Runtime/RuntimeFeatureTester.php(84): Behat\Behat\Tester\Runtime\IsolatingScenarioTester->test(Object(Behat\Behat\Context\Environment\UninitializedContextEnvironment), Object(Behat\Gherkin\Node\FeatureNode), Object(Behat\Gherkin\Node\ScenarioNode), false)
    #15 vendor/behat/behat/src/Behat/Behat/Hook/Tester/HookableFeatureTester.php(72): Behat\Behat\Tester\Runtime\RuntimeFeatureTester->test(Object(Behat\Behat\Context\Environment\UninitializedContextEnvironment), Object(Behat\Gherkin\Node\FeatureNode), false)
    #16 vendor/behat/behat/src/Behat/Behat/EventDispatcher/Tester/EventDispatchingFeatureTester.php(71): Behat\Behat\Hook\Tester\HookableFeatureTester->test(Object(Behat\Behat\Context\Environment\UninitializedContextEnvironment), Object(Behat\Gherkin\Node\FeatureNode), false)
    #17 vendor/behat/behat/src/Behat/Testwork/Tester/Runtime/RuntimeSuiteTester.php(63): Behat\Behat\EventDispatcher\Tester\EventDispatchingFeatureTester->test(Object(Behat\Behat\Context\Environment\UninitializedContextEnvironment), Object(Behat\Gherkin\Node\FeatureNode), false)
    #18 vendor/behat/behat/src/Behat/Testwork/Hook/Tester/HookableSuiteTester.php(73): Behat\Testwork\Tester\Runtime\RuntimeSuiteTester->test(Object(Behat\Behat\Context\Environment\UninitializedContextEnvironment), Object(Behat\Testwork\Specification\GroupedSpecificationIterator), false)
    #19 vendor/behat/behat/src/Behat/Testwork/EventDispatcher/Tester/EventDispatchingSuiteTester.php(72): Behat\Testwork\Hook\Tester\HookableSuiteTester->test(Object(Behat\Behat\Context\Environment\UninitializedContextEnvironment), Object(Behat\Testwork\Specification\GroupedSpecificationIterator), false)
    #20 vendor/behat/behat/src/Behat/Testwork/Tester/Runtime/RuntimeExercise.php(71): Behat\Testwork\EventDispatcher\Tester\EventDispatchingSuiteTester->test(Object(Behat\Behat\Context\Environment\UninitializedContextEnvironment), Object(Behat\Testwork\Specification\GroupedSpecificationIterator), false)
    21 vendor/behat/behat/src/Behat/Testwork/EventDispatcher/Tester/EventDispatchingExercise.php(70): Behat\Testwork\Tester\Runtime\RuntimeExercise->test(Array, false)
    #22 vendor/behat/behat/src/Behat/Testwork/Ordering/OrderedExercise.php(80): Behat\Testwork\EventDispatcher\Tester\EventDispatchingExercise->test(Array, false)
    #23 vendor/behat/behat/src/Behat/Testwork/Tester/Cli/ExerciseController.php(149): Behat\Testwork\Ordering\OrderedExercise->test(Array, false)
    #24 vendor/behat/behat/src/Behat/Testwork/Tester/Cli/ExerciseController.php(108): Behat\Testwork\Tester\Cli\ExerciseController->testSpecifications(Object(Symfony\Component\Console\Input\ArgvInput), Array)
    #25 vendor/behat/behat/src/Behat/Testwork/Cli/Command.php(63): Behat\Testwork\Tester\Cli\ExerciseController->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #26 vendor/symfony/console/Command/Command.php(251): Behat\Testwork\Cli\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #27 vendor/symfony/console/Application.php(946): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #28 vendor/symfony/console/Application.php(248): Symfony\Component\Console\Application->doRunCommand(Object(Behat\Testwork\Cli\Command), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #29 vendor/behat/behat/src/Behat/Testwork/Cli/Application.php(124): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #30 vendor/symfony/console/Application.php(148): Behat\Testwork\Cli\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
    #31 vendor/behat/behat/bin/behat(34): Symfony\Component\Console\Application->run()
    #32 {main}

    — Failed scenarios:

    features/global.feature:5
    

    1 scenario (1 failed)
    4 steps (3 passed, 1 failed)
    0m4.65s (14.16Mb)

These two questions are probably best asked (separately) on Stack Overflow, as they are not CI specific. There’s no reason you can’t ask them here, but this is a low-traffic forum and you’ll get more eyeballs on a busier site.

In general, “it works on my machine” may point to a CI problem, or it may point to the CI environment being different to your machine. Mostly it is the latter, and your task in such circumstances is to find out how it is different.

Work out how they would get there in a local context, and then transfer that to your build environment. For example, if settings.php is committed in a fresh/blank state, do you have to copy over something like settings.php.example in a prior CI step? Do you have to inject database settings from your CI env vars?

What browser driver are you using? If you’re using Selenium/Chrome etc then can you take a screenshot of the page before the failing test? Install nano in your post-fail SSH session, edit the PHP test, and run it from the console. (This is much faster than committing, pushing and waiting for a full build).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.