10.2 单体测试

Drupal 龦龢龡龝龛 PHPUnit 龅龄龂龁齾齺齹齶齳齲齰齯齭齶齩齥齣齡齠齾齭齝齙 PhoStorm 龂龁齾齋齇齃鼿鼼鼻鼹鼷鼹鼳鼲鼯鼭齩

何时你应该创建单体测试?

鼩鼧鼤鼢鼞鼛鼚鼙鼗鼯鼭龂鼕鼒齲齣鼏齠鼌鼒鼉鼅鼄鼃齩鼁鼀鼯黼黹鼢黷黵黱黰齩黯黫鼯鼭齾黧黤鼢鼩鼧黣黢龂黟黛黚黙齺齹黗黓鼯鼭龂鼳黑黎鼹鼯鼭齇黊黇黆鼢齙齩

在 PhpStorm 内配置 PHPUnit

齰齯黅黃麿

  1. 麻麸麷 PhpStorm > Preferences > Languages & Frameworks > PHP > PHPUnit > Local
    Window 麎麋麈麆麅麿File > Settings …
  2. 麀鹼 PHPUnit Library > Use custom autoloader
  3. 麻麸麷 /vendor/autoload.php
  4. 麀鹼 Test Runner > Default configuration file
  5. 麻麸麷 /core/phpunit.xml.dist
  6. 鹨鹥 Save

鹢鹠麅鹜鹚黅黃黎鼹黧齠齯齩齾齇齃鼃鹘鹢黣鹔麷 For current project 齩鹎鹊鹉鹇鹃鹊鹠黧鹁鸾麿

  1. 鼷鼹麿 Run > Edit Configurations
  2. 鹨鹥 + 鸳鸲龂麀鹼 PHPUnit
  3. 鸮鸫龝龛鸧鸦龂鸤鸣 Drupal 8: All Tests
  4. 鼃齰齯鸕鸔鸒麀鹼 Test Runner > Defined
  5. 齳鼄

鼃鹘鹢鸐龂齶齳麀鹼龡齾黧鼷鼹齰齯龂鸎鹊鸋鸈鸅鸁齩齋齇齃麎麋 Run > Run Drupal 8: All Tests

鸣鷿鷽齠齯鷺鷸鷵鷱龂齇齃鷭 File > Default Settings 龂龁鹚鷬 Drupal 8 黅黃鼳鷩麅鷨鷧黧齩

鷥齾鼷鼹鼯鼭鷢龂齾鷞鼃 PhpStorm 鸒鹔麷龝龛鷝鷚鷖龂鷔鷐龡鼯鼭鷍鷿齩鷵鷱鷉鷅龂鷁鶽黧鼯鼭鷩鷞鶺鶷鷐鹉齩齾齇齃麎麋鷚鷖鶵鹢鸐黧 Hide passed 鸳鸲鶺鶷鷁鶽黧鼯鼭齩

鼯鼭龝龛鶴鶳鶰鶮

PHPUnit 鶫鼷鼹黷黵鼯鼭齩齹鶪鼯鼭鶰鶮鶧鶣鼒龝龛鸕鸔鶠鶞齥齣黧鶛鶘鶕齇齃鶔鹊鹠鼳黑鼚麿

  1. 鼷鼹麿 Run > Edit Configurations
  2. 鹨鹥 + 鸳鸲麀鹼 PHPUnit
  3. 鸮鸫龛鸧鸦龂鸤鸣 Drupal 8: Test Example
  4. 麀鹼 Test Runner > Test Scope > Directory
  5. 鶒鶏 test_example 鶛鶘
  6. 鹨鹥 Save

说明:

如果提示“Interpreter is not specified or invalid”, 要去 PhpStorm > Preferences > Languages & Frameworks > PHP 设置下 PHP 解释器。

添加单体测试

齹鶋鶈鼙鼗鼯鼭龂齾鷨鷧鼃 tests/src/Unit/ 鸒鶋鶈龝龛齃 Test.php 鷍鶆黧鸕鸔齩鶔鶃黵鶁 Drupal 8 鶀鵼鵹黑龂齥齣鷁鶽鶋鶈龝龛鵷鵵 UnitTestCase 黧鵲鶋鶈鼯鼭齩齥齣齋齝齙 Drupal\Tests\[module_name]\Unit 麋鼒黤鼢鼯鼭鵲黧鸧鵦鵣鵟齩

tests/src/Unit/TestExampleConversionsTest.php 鸕鸔麿鶞鵞鵛鵗鸒鵕鶰鶕

  • 鼯鼭黣黢
    鼃鼳龛鼯鼭鵓龂齥齣鵑鶋鶈龡龝龛鼯鼭鵐龝龛 setUp() 黣黢齩鵊龛鼯鼭麅龝龛齃 test 鵈鸁黧黣黢齩
  • 鵇鵅
    PHPUnit 鼷鼹鵊龛鵄鶁龂鼳鼲鵄鶁鵁鴽鴺鴹鼷鼹鵇鵅黧鴸鴶齩鵇鵅麅鼯鼭鷍鷿龂鴳鶳鼯鼭麅鴱鷁鶽齩鼢黷黵鵇鵅鵄鶁龂鸣 assertTrue assertFalse assertEquals 鴪鴨鴧鴦鶁麅鴱鴣鴠齩鴝鴜黧鷍鷿麋鼒鴙龝龛鴦鶁龂鼯鼭鷍鷿麋鼒鴙鴖龛鴦鶁齩鸤鴕鵓龂齥齣鼯鼭鼩鼧鶪 0 鴓鴑鴏鴌鴋鼒 32 鴁鴑鴏齩
  • setUp()
    setUp 黣黢鼃鵊鴀鼯鼭黟鳽鷞鼷鼹龝鴀齩齥齣齝齙齲鶋鶈龝龛鵲鳼鳹鶞鳸鼯鼭黧鼩鼧黧鳴鸤鶕龂鳳鳲鳱黧鼯鼭齇黊齺齹鳳鳲鳱黧 set-up 黣黢齩
<?php

/**
 * @file
 *
 * Contains \Drupal\Tests\test_example\Unit\TestExampleConversionsTest.
 */

namespace Drupal\Tests\test_example\Unit;

use Drupal\Tests\UnitTestCase;

/**
 * Demonstrates how to write tests.
 *
 * @group test_example
 */
class TestExampleConversionsTest extends UnitTestCase {

  /**
   * @var \Drupal\test_example\TestExampleConversions
   */
  public $conversionService;

  public function setUp() {
    $this->conversionService = new \Drupal\test_example\TestExampleConversions();
  }

  /**
   * A simple test that tests our celsiusToFahrenheit() function.
   */
  public function testOneConversion() {
    // Confirm that 0C = 32F.
    $this->assertEquals(32, $this->conversionService->celsiusToFahrenheit(0));
  }

}

运行单体测试

鼃 PhpStorm 鵓齾齇齃齝齙 Run 鸎鳯鼷鼹鼙鼗鼯鼭齩鵑鼯鼭鼳龛鶛鶘黷鳬齩

添加较复杂的单体测试

鼙鳫鼯鼭鷞鳼鳩鴣鷥鳲鳱齩鳨鳥鳤鳲黧鳣鳡黧龝龛鳝鼙黧黣黢麅齝齙 setUp 黣黢龂鳛龝龛黣黢麅齝齙龝龛鶁黙鸫鵁鳗齩

鶁黙鸫鵁鳗

PHPUnit 鳕黧鶁黙鸫鵁鳗麅鼒鼯鼭黣黢鸮鸫齰齯黧黣黢齩鸫鵁鳗鳒鳎龝龛鼯鼭齙鸤鶁鳊齩

龄鸤鳕龂齥齣鶋鶈龝龛鶁黙鸫鵁鳗 providerCentimetersToInches 齩鶞鵞鵛鵗鸒鵕鶰鶕

  • 鶁黙鸫鵁鳗鳇鳄
    鼒龡鳃鹎鸫鵁鳗龂齥齣鼃鼯鼭黣黢鹢齝齙 @dataProvider 鳇鳄齩
  • 鶁黙鸫鵁鳗鴦鶁
    齥齣鲾鼃齭鼃齝齙龝龛鶁黙鸫鵁鳗龂齥齣齇齃鲼鼯鼭黣黢黗黓鴦鶁齩齥齣鼃鶁鳊鸒鲺鲷鴧龛鲳龂黤齃齥齣黧鼯鼭黣黢鵁鴽鼢鴧龛鹚鵁黧鴦鶁齩
/**
* Provides data for the testCentimetersToInches method.
*
* @return array
*/
public function providerCentimetersToInches() {
  return [
    [2.54,1],
    [254,100],
    [0,0],
    [-2.54,-1],
  ];
}

/**
* Tests centimetersToInches method.
*
* @dataProvider providerCentimetersToInches
*/
public function testCentimetersToInches($length, $expectedValue) {
  $this->assertEquals($expectedValue, $this->conversionService->centimeterToInch($length));

单体测试其他信息

PHPUnit 鲯鲬黷黵 鲫鲪鶀鵼 齩鷥齾黧鼩鼧鲨鲦鷬鲫鲪鵲鷢齾齇齃齝齙 Doubles龂mocks 鵐 stubs 齩齾齋齇齃鷔鲥鲨鲦鷬鲫鲪鼯鼭黧鼯鼭齩齝齙 @depends 鷞鴠鲡鲨鲦鼯鼭鷁鶽黛鼳龛鼯鼭鲠鼷鼹齩

评论 (写第一个评论)