add a way to manually use the test site from a Functional test

Created on 29 April 2021, over 3 years ago
Updated 14 August 2024, 25 days ago

Problem/Motivation

When developing a test, it's easy to get stuck in a situation where the setup of the test is going wrong.

Manipulating a Drupal site at one remove, through the intermediary of browser requests in the test, is a difficult and slow process. Each time a change is made, the test must be run all over again.

This could be improved if there was a way to preserve the test site after the test has run, so that the developer can interact with it and debug it.

Steps to reproduce

Proposed resolution

- Add a switch property to the Functional test base class which preserves the database tables in the test tear-down
- Detect either this or an environment variable to preserve the site in tearDown().
- Document in example.sites.php how to access a preserved test site

The developer wishing to use the test site would then:

1. Override the switch property in their test class, setting it to TRUE
2. Set the switch in their settings.local.php
3. Run the test
4. After the test is complete, go to their site, with a query string ?testsite=test12345 where 'test12345' is the name of the folder in sites/simpletest that was created for the test

Remaining tasks

User interface changes

API changes

- new class property in the Functional test base class
- new setting in settings.local.php

Data model changes

Release notes snippet

Feature request
Status

Needs review

Version

11.0 🔥

Component
PHPUnit 

Last updated 1 minute ago

Created by

🇬🇧United Kingdom joachim

Live updates comments and jobs are added and updated live.
  • Needs documentation

    A documentation change is requested elsewhere. For Drupal core (and possibly other projects), once the change has been committed, this status should be recorded in a change record node.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States smustgrave

    Could the MR be updated for 10.1 please.

  • Pipeline finished with Failed
    about 2 months ago
    Total: 643s
    #220726
  • Status changed to Needs review about 2 months ago
  • 🇬🇧United Kingdom joachim

    Added control of this feature with a BROWSERTEST_PRESERVE_SITE env var, and added documentation to phpunit.xml.dist.

  • Pipeline finished with Failed
    about 2 months ago
    Total: 590s
    #220928
  • Pipeline finished with Failed
    about 2 months ago
    Total: 705s
    #220945
  • Pipeline finished with Success
    about 2 months ago
    Total: 482s
    #221023
  • Status changed to Needs work about 1 month ago
  • 🇺🇸United States smustgrave

    Sounds like a useful idea.

    I usually have to look at screenshots in the pipeline to see what went wrong.

    Reading the comments not 100% sure how to get it working but maybe the CR will help clear that up.

  • 🇳🇿New Zealand quietone New Zealand

    Let's have this documented in the wiki and the change record can just refer to that. I think this is the page, PHPUnit Browser test tutorial .

  • 🇬🇧United Kingdom joachim

    Proposed documentation text:

    Interacting with the test site

    The test site can be preserved after the test has run, to allow the developer to interact with it as with a normal site and to inspect the database tables. This is useful for debugging and developing tests, when the HTML output files are insufficient to understand a problem.

    To preserve a test site instead of having it be deleted in the test’s tear down phase, do one of the following:

    • Set the class property $preserveSiteInTearDown to TRUE in your test class
    • Set the environment variable BROWSERTEST_PRESERVE_SITE to ‘true’ on the command line when running phpunit
    • Set the environment variable BROWSERTEST_PRESERVE_SITE to ‘true’ in your phpunit.xml file

    Then, after the test has run, do the following:

    1. Find the test site prefix. You can get this from the test site directory which was created during the test in your sites/ directory. It will be of the form ‘simpletest/test_123456'. The ‘test_123456’ is the test site prefix.
    2. Set up the sites.php file if it is not already present.
    3. Add code to return the test site directory as the site. The simplest way is to completely replace the main site with the test site:
      $sites = [
      ‘example.com’ => ‘simpletest/TEST_PREFIX',
      ];</ol>
      

    You can then use your site as you would a normal Drupal site. The admin user login is ‘admin’/‘admin’.

    You will need to delete the site directory and the test site database tables manually.

    —————-

    TODO: it occurs to me writing these instructions that it would be a nice DX touch to output the test site prefix in the phpunit output when the site is being preserved.

    PS. It’s not a wiki!!!

  • Status changed to Needs review about 1 month ago
  • 🇬🇧United Kingdom joachim

    Wrote CR & draft documentation.

    Ready for review again.

  • Status changed to RTBC about 1 month ago
  • 🇺🇸United States smustgrave

    Read the Cr and feel comfortable removing that tag.

    Leaving the documentation tag as don't know the process for getting that page updated.

    But thinks seem clearer based on what's there. Going to mark.

  • Status changed to Needs work 25 days ago
  • 🇳🇿New Zealand quietone New Zealand

    I have been wanting to test this and today I finally did. I am pleased to say that it works in my environment which is DDEV and with a separate database for testing.

    TODO: it occurs to me writing these instructions that it would be a nice DX touch to output the test site prefix in the phpunit output when the site is being preserved.

    Yes, that would be a nice to have.

    The text in #14 for the doc page is fine with me. It does help to see it here though instead of waiting for this to be committed.

    I left comments and suggestions in the MR, about the comments, that do need to be looked at.

  • Pipeline finished with Canceled
    25 days ago
    Total: 118s
    #253657
  • Pipeline finished with Canceled
    25 days ago
    Total: 200s
    #253658
  • Pipeline finished with Canceled
    25 days ago
    #253668
  • Pipeline finished with Failed
    25 days ago
    #253671
  • Status changed to Needs review 25 days ago
  • 🇬🇧United Kingdom joachim

    > Yes, that would be a nice to have.

    Done, but I suspect this is going to bog this issue down, as the way I've done it isn't ideal and I don't see a better way.

    It's not ideal because printing from within the test process causes PHPUnit to say the test was risky because it printed something. I think that's acceptable, because the developer knows they're doing something for debugging.

    I experimented with using a PHPUnit extension, same way that HtmlOutputLogger is a PHPUnit extension, but that doesn't work because outside of the test's process, we can't get the site ID.

    The only way that I can think of would be to either write the ID to a file, or have the PHPUnit extension look at all the folders in sites/simpletest and pick the newest one.

  • Pipeline finished with Failed
    25 days ago
    Total: 501s
    #253682
Production build 0.71.5 2024