沃梦达 / 编程问答 / php问题 / 正文

PHPUnit中两个TestCase类之间的依赖测试

Dependent tests between two TestCase classes in PHPUnit(PHPUnit中两个TestCase类之间的依赖测试)

本文介绍了PHPUnit中两个TestCase类之间的依赖测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

In PHPUnit you can make one test to be depend on other test by using @depends annotation. Is it possible to make whole TestCase dependent on test in other TestCase? Or at least make single test in one TestCase dependent on test in other TestCase?

I tried:

/**
 * @depends A::testMethodName
 */

But as I expected it doesn't work.

Update:

The exact situation looks like this: There is class B which uses class A. So I want to test B only if the tests for A (or one of it's tests) run without a failure. How can I do that?

解决方案

Exploiting dependencies are extremely important! The loose coupling as referred to is for actual application architecture not for unit test cases. If there is logical dependencies built in to functional execution is is always a good idea to exploit those dependencies.

Using test doubling is appropriate for SOA where dependencies cannot be mapped to a particular failure within a black box AND the service is not reliable. This is not appropriate for inter application classes.

You definitely would want to use this type of functionality if there is logical dependencies between test classes. The concept to be grasped from unit testing is it's ability to isolate defects to particular components immediately.

This functionality IS available on PHPUnit v 3.7.13. However, the only way this will work is if you run PHPUnit on a directory which contains both TestCase classes.

For example with this folder structure

- applicationdep
   |- BTest.php
   |- CTest.php

The classes...

class BTest extends PHPUnit_Framework_TestCase
{
    /**
     * @depends CTest::testADomino
     */
    public function testDominoDependent()
    {
        $this->assertTrue(true);
    }
}

and...

class CTest extends PHPUnit_Framework_TestCase
{
    public function testADomino()
    {
        $this->assertTrue(false);
    }
}

This is the result

C:UsersJosh>C:xamppphpphpunit.bat "C:xampphtdocsBeAgileapplicationssto
cklogger	estsdep"
PHPUnit 3.7.13 by Sebastian Bergmann.

SF

Time: 0 seconds, Memory: 2.00Mb

There was 1 failure:

1) CTest::testADomino
Failed asserting that false is true.

C:xampphtdocsBeAgileapplicationsstocklogger	estsdepCTest.php:7

FAILURES!
Tests: 1, Assertions: 1, Failures: 1, Skipped: 1.

You could have both test case classes in the same file but that would be a poor structure. It is not necessary to "make sure" one test runs before the other.

As an agile coach I see test doubling far too often in large organizations where specialized segments want to avoid having build failures when another components makes changes that causes test failure. This of course defeats the entire purpose of the unit tests which is to identify component failures before the end user does.

这篇关于PHPUnit中两个TestCase类之间的依赖测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:PHPUnit中两个TestCase类之间的依赖测试

基础教程推荐