PHPUnit中两个TestCase类之间的从属测试

编程入门 行业动态 更新时间:2024-10-10 21:24:11
本文介绍了PHPUnit中两个TestCase类之间的从属测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在PHPUnit中,您可以使用@depends注释使一个测试依赖于其他测试. 是否可以使整个TestCase依赖于其他TestCase中的测试?还是至少要在一个TestCase中进行单个测试,而依赖于其他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?

我尝试过:

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

但是正如我预期的那样,它是行不通的.

But as I expected it doesn't work.

更新:

确切的情况是这样的:有class B使用class A.因此,我只想在A的测试(或其中一项测试)没有失败的情况下测试B.我该怎么办?

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.

使用测试加倍适用于依赖项的SOA无法在黑框内将其映射到特定故障,并且服务不可靠.这不适用于应用程序间的类.

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.

此功能在PHPUnit v 3.7.13上可用.但是,唯一可行的方法是在包含两个TestCase类的目录上运行PHPUnit.

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

- application\dep |- BTest.php |- CTest.php

课程...

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

然后...

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

这是结果

C:\Users\Josh>C:\xampp\php\phpunit.bat "C:\xampp\htdocs\BeAgile\applications\sto cklogger\tests\dep" 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:\xampp\htdocs\BeAgile\applications\stocklogger\tests\dep\CTest.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:51:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/471227.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   测试   PHPUnit   TestCase

发布评论

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

>www.elefans.com

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