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 鳵鳴齮鳱鳯鼼鼹鼐鼳龩齉鷼齺黴鼟鼜鳫鷏鳧鷼齂

评论 (写第一个评论)