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 鴀鳼齦鳹鳸齂鼾鼑鼸龠齋鷫齴黭鼡鼝鳶鷀鳴鷫齄

评论 (写第一个评论)