10.3 功能测试

Drupal 8 龧龤龣龡龟龝龙龘龔龓龒龑龍龊龇龅龤龁齿齻齺齹齵齱齮 SimpleTest齝齙齕齑齏 齋齉齈齄龟龝龧齁鼿鼽鼼鼸鼷齝user齏鼳鼲鼰鼮龒龑鼫鼨齻鼧鼣鼢鼞鼸鼷鼜鼘鼖龊鼒龑鼫鼨齈齁鼿龙龘鼐鼸龣龡龟龝齻鼏鼍齁鼿龙龘鼉鼇 Drupal 鼅鼁鼸鼷黿黻齈黷黵黳黰黯鼲龟龝鼢鼞鼒龑黮黭黪齻黩齁鼿黦黤黠黝龤鼜黚默黗龤龒龑黓黑齈

PHPUnit 和 SimpleTest 功能测试(BrowserTestBase / WebTestBase)

黍黉黅黁麾龤麽麹龣龡龟龝黓黑鼜鼐鼸 PHPUnit 黅麪麦 BrowserTestBase 齈黚麝齙齕鼐鼸麙龤龣龡龟龝黓黑 SimpleTest 黪麕 WebTestBase 齈麪麏 PHPUnit 龤龟龝龡麍 IDE 龇龅齈SimpleTest 龟龝龇龅麆默黗龒麅齻齁鼿麁龊鼣麀鹿鹼鹻齈

黷鹸 BrowserTestBase 龤鹶龘鹳鹲鼜鹱龡鼐鼸 JavaScript 黅 AJAX 齈鹣鹢龔龤龟龝龙龘鹠鼿齻鹞鹜龊 JavaScript 鹙鹕鼮鹻 BrowserTestBase 鼧鹸鹑鼁鼐鼸麙龤 WebTestBase

鼰鼮鼸鼷鼫鼨

鹍龒龑龟龝齻齁鼿鼼鼸鼷鼳鼲鼰鼮龒龑鼫鼨齻鼧鼣龟龝鼒龑鼫鼨鼜鼘龓鹊齈

test_example.info.yml (鸶鸲鸱齑鸮鸭)

name: Test Example
type: module
description: Example showing how to create tests
core: 8.x
package: Examples
dependencies:
  - user
  - options

config/install/field.field.user.user.test_status.yml

鼐鼸鼒龑鸞鸚鸙鸘鼫鼨鸗齝鸶鸲鸱齑鸮鸭齏

langcode: en
status: true
dependencies:
  config:
    - field.storage.user.test_status
id: user.user.test_status
field_name: test_status
entity_type: user
bundle: user
label: test_status
description: ''
required: false
translatable: false
default_value:
  -
    value: 0
default_value_callback: ''
settings:
  on_label: 'On'
  off_label: 'Off'
field_type: boolean

config/install/field.storage.user.test_status.yml 齝鸶鸲鸱齑鸮鸭齏

鼰鼮龒龑龣龡龟龝

龘鸖鸒龣龡龟龝齻龊 tests/src/Functional/ 齑鸖鸒龒龑黪 Test.php 鸉鸇龤鸞鸚齈黻鸅麝鸃 Drupal 8 鷿鷾鹞鷽齻齁鼿黳黰鸖鸒龒龑鷺鷷 BrowserTestBase 龤麦鸖鸒龟龝齈齁鼿黮鼐鼸 Drupal\Tests\[module_name]\Functional 鷭鼍鷬龓龟龝麦龤鷪鼫鷨鷧齈

鼒龑齋鷥黅黯鼲龟龝黚黻齻鷤鼜鷡鷞鷛黅鷪鼫鷨鷧龧龤 Unit 鷙鷕鼍 Functional

langcode: en
status: true
dependencies:
  module:
    - user
id: user.test_status
field_name: test_status
entity_type: user
type: boolean
settings: {  }
module: test_example
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false

src/Tests/TestExampleUserTest.php 齝鸶鸲鸱齑鸮鸭齏

  • 鷓鷐
    鷬龓鼐鼸 PhpUnit 龤龣龡龟龝龙龘龓鷏龑鷓鷐齻 @runTestsInSeparateProcesses @preserveGlobalState disabled 齈
  • 鷆鷂龤齋齉
    鼏鼍龣龡龟龝龇龅龊鷀龓鶾鶺鶹鸒龤 Drupal 黓黑齑齻鷬黪齁鼿龙龘鶵鶳鷬龓鶯鶫齈齁鼿鶵鶳齋齉齻鼒鷽龟龝鶪鶦鶢龘鷆鷂鹠齈
  • setUp
    齁鼿龤龟龝鶟齻龓鶞龘鶝鼸 parent::setUp齻鼏鼍鼒麅龣龡龟龝龤 setup 鶙黷默黗齈齁鼿黮龡鶝鼸鷿鸙龤 drupalCreateUser() 鶗鶖齻鹠鶔鶒齁鼿鶏鶍鼸鼷鼳鼲齈
  • 龟龝鶗鶖
    黅黯鼲龟龝龒鷽齻鶗鶖黪 test 鶉鶅齈
  • 鶃鵿鵽鵼
    鶃鵿鵽鵼黅 PHPUnit 黯鼲龟龝鶙鵺齈鼒龑鵹鵸鶟齻齁鼿鼢鼞鼜鼘鼸鼷鼳鼲鼖龊龒龑鼫鼨齈鼸鼷鼳鼲龓龒龑齁鼿黭黪鼐鼸龤 getFieldDefinitions() 鶗鶖齈鼒黿麏黯鼲龟龝龓鹊齻黩鹠麽麹鵵鵲 null齈齁鼿龊龟龝鼒龑鼫鼨鼜鼘鼖龊齻鷬龓齁鼿鹱鵯齋鵬鼒龑鵨鸃齻齁鼿鵯鶦鶢鼳鵦鵢鹠鼜鼘鵵鵲齁鼿龤鼫鼨齈

<?php

/**
 * @file
 *
 * Contains \Drupal\Tests\test_example\Functional\TestExampleUserTest.
 */

namespace Drupal\Tests\test_example\Functional;

use Drupal\simpletest\BrowserTestBase;

/**
 * Check if our user field works.
 *
 * @group test_example
 * @runTestsInSeparateProcesses
 * @preserveGlobalState disabled
 */
class TestExampleUserTest extends BrowserTestBase {

  /**
   * @var \Drupal\user\Entity\User.
   */
  protected $user;

  /**
   * Enabled modules
   */
  public static $modules = ['test_example'];

  /**
   * {@inheritdoc}
   */
  function setUp() {
    parent::setUp();

    $this->user = $this->drupalCreateUser();
  }

  /**
   * Test that the user has a test_status field.
   */
  public function testUserHasTestStatusField() {
    $this->assertTrue(in_array('test_status', array_keys($this->user->getFieldDefinitions())));
  }

}

运行这个功能测试

龘龇龅龣龡龟龝 齻齁鼿龙龘鵟鵞 PHPUnit 鵛鵘鶟鶏鶍齁鼿龤龁齿齈龊 PhpStorm 龧黳黰鵖鵔龒龑鵑鵏鵍鵊鼳鵉鼒龑齈

  1. 鵅鶉 PhpStorm
  2. Command Line 鴿鴽齻鴼鴸 Environment 鵍鵊齈
  3. 鼸鷪 SIMPLETEST_BASE_URL 黅鴰齝龁齿URL齏鹕鼮龒龑鴬鴨鴥齈

龔龡鼐鼸 Run 鴤鴡齻黻龇龅黯鼲龟龝鹞鷽龇龅鼒龑龟龝齈

ConfigurePhpUnitBrowserTestBase

AJAX 遗留功能测试(SimpleTest)

黅 PHPUnit 龣龡龟龝龒鷽齻鹣鹢龔鼐鼸 AJAX 齻龊 JavaScript 鼰鼮鹻 BrowserTestBase 鼧鹸鷤龡鼐鼸 SimpleTest 黓黑齈龔龙龘龓龒龑齵齱齮 SimpleTest 齋齉龤龁齿齈

PHPUnit 龣龡龟龝黅 SimpleTest 龣龡龟龝龤鸭鴞

鷏鴜龟龝麦鴙龓鶙鴘鼧黠齻黩黷龔龙龘鼐鼸鷏鴗鴖齻鴒鴐鴎鴊鴈鴇鴃鳿齈

鸞鸚鳽鷪黅鷪鼫鷨鷧

龘鸖鸒 SimpleTest 龣龡龟龝齻龔鶞鳻龊 src/Tests/ 齑鸖鸒龒龑黪 Test.php 鸉鸇龤鸞鸚齈黻鸅麝鸃 Drupal 8 鷿鷾鹞鷽齻齁鼿黳黰鷺鷷麪麦 WebTestBase 鳺 KernelTestBase 鸖鸒龟龝齈齁鼿黮鼐鼸 Drupal\[module_name]\Tests 鷭鼍鷬龓龟龝麦龤鷪鼫鷨鷧齈鼒龑齋鷥黚黻 PHPUnit 龟龝齻黩鹱黉鳳齈PHPUnit 龣龡龟龝鳰麏 tests/src/Functional 齑齻鳭 SimpleTest 龟龝龊 src/Tests 齑 齈鵺鷽齻鷪鼫鷨鷧黅麪麦黮鹱鶙鵺齈

龟龝鸞鸚鵹鵸 src/Tests/TestExampleUserTest.php 鸗齝鸶鸲鸱齑鸮鸭齏

<?php

/**
 * @file
 *
 * Contains \Drupal\test_example\Tests\TestExampleUserTest.
 */

namespace Drupal\test_example\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Check if our user field works.
 *
 * @group test_example
 */
class TestExampleUserTest extends WebTestBase {

  /**
   * @var \Drupal\user\Entity\User.
   */
  protected $user;

  /**
   * Enabled modules
   */
  public static $modules = ['test_example'];

  /**
   * {@inheritdoc}
   */
  function setUp() {
    parent::setUp();

    $this->user = $this->drupalCreateUser();
  }

  /**
   * Test that the user has a test_status field.
   */
  public function testUserHasTestStatusField() {
    $this->assertTrue(in_array('test_status', array_keys($this->user->getFieldDefinitions())));
  }

}

额外的功能测试目标

Drupal 齙齕鶟龓黚麝龣龡龟龝齈

core/modules/user/src/Tests/UserBlocksTest.php 鳪鳦齮鳢鳡齁鼿鼐鼸龤齋鷥齻黮鼢鼞鳝鶳鳚鷥齈

评论 (写第一个评论)