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 鸀鵈鲾鳄鳂鼾鼺鷪鷩黩齂龞鼾鼺鲻齅齈齫

评论 (写第一个评论)