热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHPUnit:从setUp()获取测试类和方法的名称?-PHPUnit:GetnameoftestclassandmethodfromsetUp()?

PHPUnitrunsthesetUp()methodofatestclasspriortorunningaspecifictest.PHPUnit在运行特定测试之前运行

PHPUnit runs the setUp() method of a test class prior to running a specific test.

PHPUnit在运行特定测试之前运行测试类的setUp()方法。

I load up test-specific fixtures for every test in a test class and would prefer not to have to explicitly do so. I would ideally like to handle this automagically in the setUp() method.

我为测试类中的每个测试加载特定于测试的灯具,并且不希望明确地这样做。理想情况下,我想在setUp()方法中自动处理这个问题。

If the setUp() method makes available the test class name and test method name this can be done.

如果setUp()方法提供测试类名称和测试方法名称,则可以执行此操作。

Is the name of the test class and method that is about to be run available to me in the setUp() method?

是否在setUp()方法中可以运行的测试类和方法的名称?

2 个解决方案

#1


28  

The easiest way to achieve this should be calling $this->getName() in setUp().

实现这一目标的最简单方法应该是在setUp()中调用$ this-> getName()。

getName());
    }


    public function testMethod()
    {
        $this->assertEquals(4,2+2,'OK1');
    }
}

And running:

phpunit MyTest.php 

produces:

PHPUnit 3.7.1 by Sebastian Bergmann.

.string(10) "testMethod"


Time: 0 seconds, Memory: 5.00Mb

OK (1 test, 1 assertion)

In general I'd advice against doing this but there sure are cases where it can be a nice way to do things.

一般来说,我建议不要这样做,但确实有一些情况可以成为一种很好的做事方式。

Other options would be to have more than one test class and having all the tests that use the same fixtures together in one class.

其他选项可能是拥有多个测试类,并且所有测试都在一个类中使用相同的灯具。

Another would be to have private setUp helpers and calling the appropriate one from the test case.

另一种方法是拥有私有的setUp助手,并从测试用例中调用适当的助手。

#2


2  

Alternatively, if you don't want to show the string(10) part like in the answer of edorian you can do it like this:

或者,如果您不想像edorian的答案那样显示字符串(10)部分,您可以这样做:

protected function setUp()
{
    echo $this->getName() . "\n";
}

推荐阅读
author-avatar
Boss-201411
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有