Dependent tests between two TestCase classes in PHPUnit(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类之间的依赖测试


- 没有作曲家的 PSR4 自动加载 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- Laravel 仓库 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01