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 鷳鵄鲮鲴鲱齇齅鷛鷗黣齊龤齇齅鲫齍齏齵

评论 (写第一个评论)