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 鷲鴵鲕鲞鲜鼳鼲鷠鷟黛鼶龞鼳鼲鲔鼸鼻齦

评论 (写第一个评论)