php Phpunit 测试给出警告没有在课堂上找到测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27599100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Phpunit tests gives warning no tests found in class
提问by ytsejam
I am trying to learn how to test with phpunit and laravel. When start the test using phpunit
command, I am getting a warning :
我正在尝试学习如何使用 phpunit 和 laravel 进行测试。使用phpunit
命令开始测试时,我收到警告:
There was 1 failure:
1) Warning
No tests found in class "PostsTest".
FAILURES!
Tests: 2, Assertions: 1, Failures:
My test classname and filename matches. I have read other problems about unmatching names. my filename is PostsTest.php
and my test file :
我的测试类名和文件名匹配。我已经阅读了有关不匹配名称的其他问题。我的文件名是PostsTest.php
我的测试文件:
class PostsTest extends ApiTester {
public function it_fetches_posts()
{
$this->times(5)->makePost();
$this->getJson('api/v1/posts');
$this->assertResponseOk();
}
private function makePost($postFields=[])
{
$post = array_merge([
'title' => $this->fake->sentence,
'content' => $this->fake->paragragraph
], $postFields);
while($this->times --)Post::create($post);
}
}
if necessary my ApiTester :
如有必要,我的 ApiTester :
use Faker\Factory as Faker;
class ApiTester extends TestCase {
protected $fake;
protected $times = 1;
function __construct($faker)
{
$this->fake = Faker::create();
}
}
I dont have any clue where the error is. Laravel or my local phpunit settings or anything else. Any helps is appreciated.
我不知道错误在哪里。Laravel 或我的本地 phpunit 设置或其他任何设置。任何帮助表示赞赏。
Thanks.
谢谢。
回答by CJ Thompson
/** @test */
public function it_tests_something()
{
...
}
Adding that @test
tells phpunit to treat the function as a test, regardless of the name.
添加它@test
告诉 phpunit 将函数视为测试,而不管名称如何。
回答by gontrollez
The only methods that PHPUnit will recognize as tests are those with names starting with test.
PHPUnit 将识别为测试的唯一方法是名称以test开头的方法。
So you should rename the it_fetches_posts()
method to test_it_fetches_posts
or testItFetchesPosts
. The camel case naming is optional but useful if you use the --testdox option later.
所以你应该将it_fetches_posts()
方法重命名为test_it_fetches_posts
or testItFetchesPosts
。驼峰命名是可选的,但如果您稍后使用 --testdox 选项很有用。
Also, as stated in other answer you can also add the @test
annotation to any method and it will be considered a test by PHPUnit.
此外,如其他答案中所述,您还可以将@test
注释添加到任何方法中,PHPUnit 会将其视为测试。
回答by Yamen Ashraf
Either begin its name with word 'test' like test_something_should_work
or update the test docs with this annotation /** @test */
要么以“test”一词开头,要么test_something_should_work
使用此注释更新测试文档/** @test */
回答by shabany
Additionally, consider a case where you are testing a class A
that requires a class B
(that you will mock). When $a->someMethod($mocked_B_class)
is called, make sure you don't have any warnings in it such as trying to access a property of an array like you would access the property of a class ($array = ['one','two']; $array->one)
.
此外,请考虑您正在测试A
需要类B
(您将模拟)的类的情况。当$a->someMethod($mocked_B_class)
被调用时,请确保你没有在它的任何警告,如试图访问像你这样的阵列的属性将访问类的属性($array = ['one','two']; $array->one)
。
In this case it wont give you any information about the test or the error
在这种情况下,它不会为您提供有关测试或错误的任何信息