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 鷻鴺鲢鲫鲩鼶鼳鷩鷥黦鼺龜鼶鼳鲡鼼鼽齭

评论 (写第一个评论)