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类之间的依赖测试


基础教程推荐
- PHP 类:全局变量作为类中的属性 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01