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 鴁鳽齯鳺鳶齅齃鼜鼽龩齍鷲齾黶鼦鼥鳲鶺鳯鷲齉

评论 (写第一个评论)