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 鳫鳩齯鳨鳥齉齆鼙鼾龣齓鷠齽黰鼦鼥鳡鶻鳟鷠齎

评论 (写第一个评论)