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类之间的依赖测试
基础教程推荐
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01