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

编程入门 行业动态 更新时间:2024-10-11 13:23:09
本文介绍了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类之间的依赖测试

本文发布于:2023-06-03 09:52:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/471235.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   测试   PHPUnit   TestCase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!