Phpunit测试给出的警告是类中没有发现的测试

时间:2022-02-23 07:02:08

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 :

如果需要的话,我的助教:

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.

谢谢。

3 个解决方案

#1


75  

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或睪丸。驼色大小写命名是可选的,但是如果您稍后使用—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视为一个测试。

#2


37  

Annotations are the answer.

注释是答案。

/** @test */
public function it_tests_something()
{
  ...
}

Adding that @test tells phpunit to treat the function as a test, regardless of the name.

添加@test后,告诉phpunit将该函数视为一个测试,而不考虑名称。

#3


0  

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).

此外,考虑这样一个情况:您正在测试一个需要类B(您将对其进行模拟)的类a。当调用$a->someMethod($mocked_B_class)时,请确保其中没有任何警告,比如尝试访问数组的属性,就像访问类的属性一样($array = [' 1 ',' 2 ']);$数组- >)。

In this case it wont give you any information about the test or the error

在这种情况下,它不会给你关于测试或错误的任何信息

#1


75  

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或睪丸。驼色大小写命名是可选的,但是如果您稍后使用—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视为一个测试。

#2


37  

Annotations are the answer.

注释是答案。

/** @test */
public function it_tests_something()
{
  ...
}

Adding that @test tells phpunit to treat the function as a test, regardless of the name.

添加@test后,告诉phpunit将该函数视为一个测试,而不考虑名称。

#3


0  

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).

此外,考虑这样一个情况:您正在测试一个需要类B(您将对其进行模拟)的类a。当调用$a->someMethod($mocked_B_class)时,请确保其中没有任何警告,比如尝试访问数组的属性,就像访问类的属性一样($array = [' 1 ',' 2 ']);$数组- >)。

In this case it wont give you any information about the test or the error

在这种情况下,它不会给你关于测试或错误的任何信息