【问题标题】:phpunit test class which extends another class扩展另一个类的phpunit测试类
【发布时间】:2017-03-15 10:21:48
【问题描述】:

我是 phpunit(以及任何其他测试驱动开发)的新手。 我已经使用composer设置了phpunit,到目前为止一切都很好。

我正在尝试学习他们的教程,可以在这里找到: https://phpunit.de/getting-started.html

我的问题来了: 我有一个看起来像这样的类:

class MyClassName
    extends CoreClass
    implements ClassName

在他们的例子中,他们这样做:

class MyClassName
    extends TestCase

但这对我不起作用,因为我还必须扩展另一个类。 这是如何工作的?

我只想测试MyClassName,我还没有构建核心,所以我想离开那里。

【问题讨论】:

    标签: unit-testing testing phpunit automated-tests


    【解决方案1】:

    如果你叫你类MyClassName,那么测试类应该叫MyClassNameTest(注意Test后缀)。

    根据您要使用的命名空间(并在 composer 中配置),您的类将位于 src 目录中的某个位置,即 src/MyClassName.php

    <?php
    
    class MyClassName extends CoreClass implements ClassName
    {
    }
    

    您的测试用例将位于tests 目录中,即tests/MyClassNameTest.php

    <?php
    
    use PHPUnit\Framework\TestCase;
    
    class MyClassNameTest extends TestCase
    {
    }
    

    最流行的方法是在tests 中镜像src 目录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2012-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多