简体   繁体   English

使用PHP SimpleTest进行测试

[英]Testing with PHP SimpleTest

I'm running into an error while trying to run some simple tests with SimpleTest for PHP . 尝试使用SimpleTest for PHP运行一些简单测试时遇到错误。

Currently, I'm extending the class UnitTestCase as per the documentation. 目前,我正在根据文档扩展UnitTestCase类。 I'm trying to test different aspects of my class within a method. 我试图在一个方法中测试类的不同方面。 Here is my class: 这是我的课:

<?php
class SimpleClass extends UnitTestCase {

    public function __construct() {
        $this->test();
    }

    private function test() {
        $x = true;
        $this->assertTrue($x, 'x is true');
    }
}

I've tried extending the TestSuite class and using the syntax in the documentation but I got the same error: 我尝试扩展TestSuite类并使用文档中的语法,但遇到相同的错误:

Fatal error: Call to a member function getDumper() on a non-object in /simpletest/test_case.php on line 316

Any ideas on how I could do this or am I approaching class testing wrong? 关于如何执行此操作的任何想法,或者我即将进行类测试错误吗?

Don't use a constructor in your test! 不要在测试中使用构造函数!

SimpleTest allows you to create classes with methods. SimpleTest允许您使用方法创建类。 If their name starts with "test", it is automatically recognized as a testing method that will get called if you start the test suite. 如果它们的名称以“ test”开头,则它将自动识别为一种测试方法,如果您启动测试套件,则会调用该方法。

You created a constructor which calls your test method, and does an assertion without all the setup taking place, so SimpleTest does not have a reporter class that is needed to wrap it's test findings into a nice output. 您创建了一个构造函数,该构造函数调用您的测试方法,并在未进行所有设置的情况下进行断言,因此SimpleTest没有将它的测试结果包装到一个不错的输出中所需要的报告程序类。

Read the tutorial more closely, and you'll find some hints on how to set up a test suite or how to start a single test: 仔细阅读本教程,您会发现一些有关如何设置测试套件或如何开始单个测试的提示:

Let us suppose we are testing a simple file logging class called Log in classes/log.php. 让我们假设我们正在测试一个名为Log in classes / log.php的简单文件记录类。 We start by creating a test script which we will call tests/log_test.php and populate it as follows... 我们首先创建一个测试脚本,该脚本将调用tests / log_test.php并按如下所示进行填充...

Code example adapted from the documentation: 从文档改编的代码示例:

<?php
require_once('simpletest/autorun.php');
require_once('../classes/log.php');

class TestOfLogging extends UnitTestCase {
    function testLogCreatesNewFileOnFirstMessage() {
        $this->assertTrue(true);
    }
}
?>

Note there is no constructor, and the autorun file will take care to run the test if this file is executed with PHP. 请注意,没有构造函数,并且如果使用PHP执行该文件,则自动运行文件将小心运行该测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM