php - 如何制作 PHP 测试套件

标签 php unit-testing testing

我一直在四处寻找,但没有找到任何关于测试套件的有用信息,任何人都可以解释一下它应该是什么样子。

我找到了这个例子:

require_once 'MyTest.php';

class MySuite extends PHPUnit_Framework_TestSuite
{
    public static function suite()
    {
        return new MySuite('MyTest');
    }

    protected function setUp()
    {
        print "\nMySuite::setUp()";
    }

    protected function tearDown()
    {
        print "\nMySuite::tearDown()";
    }
}

我理解的东西很少,有些不理解,我知道我应该包含我的测试用例,比如 include 'Test.php';

我想制作一个包含我所有测试用例的测试套件,这个测试套件应该是什么样子的?我尝试了一个在 itnernet 上找到的示例,但收到了这条错误消息:

Fatal error: Uncaught exception 'PHPUnit_Framework_Exception' with message 'Argument #1 of PHPUnit_Framework_TestSuite::addTestSuite() must be a class name or object' in C:\xampp\php\pear\PHPUnit\Util\InvalidArgumentHelper.php:69
Stack trace:
#0 C:\xampp\php\pear\PHPUnit\Framework\TestSuite.php(288): PHPUnit_Util_InvalidArgumentHelper::factory(1, 'class name or o...')
#1 C:\xampp\htdocs\exem\MyTestSuite.php(13): PHPUnit_Framework_TestSuite->addTestSuite('testFunctions')
#2 [internal function]: AllTests::suite('AllTests')
#3 C:\xampp\php\pear\PHPUnit\Runner\BaseTestRunner.php(124): ReflectionMethod->invoke(NULL, 'AllTests')
#4 C:\xampp\php\pear\PHPUnit\TextUI\Command.php(150): PHPUnit_Runner_BaseTestRunner->getTest('mytestsuite', 'C:\xampp\htdocs...', Array)
#5 C:\xampp\php\pear\PHPUnit\TextUI\Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
#6 C:\xampp\php\phpunit(46): PHPUnit_TextUI_Command::main()
#7 {main}
  thrown in C:\xampp\php\pear\PHPUnit\Util\InvalidArgumentHelper.php on line 69

你能写信给我或给我看一个测试套件的例子吗?代码的样子。

我的测试文件是这样的:

include 'functions.php';

class Test extends PHPUnit_Framework_TestCase 
{

    protected function getConnection()
    {
        $mysqli = new mysqli('local', 'root', '', 'db_13839918');

        if($mysqli->connect_errno > 0){
            die('Unable to connect to database [' . $mysqli->connect_error . ']');
        }
    }

    protected function setUp()
    {
        $mysqli = new mysqli('localhost', 'root', '', 'db_13839918');

        $now = time();
        $i = 0;

        do {
            $i = $i+1;
            $mysqli->query("INSERT INTO login_attempts (user_id, time) VALUES ('4892388', '$now')");

        } while ($i < 6);
    }

    public function testCheckbrute()
    {
        $mysqli = new mysqli('localhost', 'root', '', 'db_13839918');

        $LessThanFive = checkbrute('4892389', $mysqli);
        $this->assertFalse($LessThanFive);

        $FiveFailedLogins = checkbrute('4892388', $mysqli);
        $this->assertTrue($FiveFailedLogins);
    }

    public function testLogin()
    {
        $mysqli = new mysqli('localhost', 'root', '', 'db_13839918');

        $AccountIsLockedFalse = Login("xxxxx","xxx", $mysqli);
        $this->assertFalse($AccountIsLockedFalse);

        $NoUserExists = Login("xxxxxx","xxxxx", $mysqli);
        $this->assertFalse($NoUserExists);
    }

    protected function tearDown()
    {
        $mysqli = new mysqli('localhost', 'root', '', 'db_13839918');
        $mysqli->query("DELETE FROM members WHERE id IN (9, 11)");
    }
}

最佳答案

“MyTest.php”是什么样的?

这个错误:

Fatal error: Uncaught exception 'PHPUnit_Framework_Exception' with message 'Argument #1 of PHPUnit_Framework_TestSuite::addTestSuite() must be a class name or object'

意味着这一行 return new MySuite('MyTest'); 导致了问题。

将其更改为 return new MySuite('Test'); 传递给 MySuite 的字符串需要是包含测试的类的名称。

PHPUnit 正在寻找名为 MyTest 的类,但没有找到。

关于php - 如何制作 PHP 测试套件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16655832/

相关文章:

php - 连接 2 个表的查询(使用 LIKE 和准备好的语句)

c# - 单元测试和数据库

python - 如何在 Python 中编写自定义的 `.assertFoo()` 方法?

java - 如何找到错误 TCP 连接的原因

PHP - Curl 正在添加 HTTP ://to my HTTPS URL

php - 德鲁帕尔 6 : Make custom function available to different modules

c++ - eclipse CDT : how to test functions?

java - 测试 Web 服务的工具和最佳实践是什么?

用于 Code igniter 代码的 PHP 混淆器

sql - 如何测试 SQL "CREATE TABLE"语句是否等效于现有表?