【问题标题】:Can I use phpunit tests on functions that are not in class?我可以对不在课堂上的函数使用 phpunit 测试吗?
【发布时间】:2020-05-01 21:58:08
【问题描述】:

我有一个小的.php 文件,其中包含我在下面粘贴的那个函数。我想测试一下这个功能。

有什么方法可以使用 phpunit 吗?我没有使用composer.json

<?php public function LoginMe($a, $b) //nick, psw
        {
        require 'includes/mysql_connection.php';
        require 'includes/config.php';
        require 'includes/messages.php';
        require 'backend/LogsSystem.php';

                // Some code

                return false;
        } ?>

【问题讨论】:

  • 是的,您可以使用 Phpunit 为函数编写单元测试。它就是为此而生的。但是,它需要您首先设置 Phpunit 的工作版本。然后检查您的测试套件引导过程是否涵盖了所有需要的初始化(或使用类方法之前的设置),以便该功能可用。当您需要或包含文件时,请注意副作用。正是这些副作用通常会阻止其他部分(这里:测试套件或运行器)正常工作。调试此类问题是值得的,这样您就可以更轻松地了解副作用并解决它们。

标签: php phpunit


【解决方案1】:

例如,如果你的脚本被称为authentication.php,这可能是一个测试提示:

<?php
use PHPUnit\Framework\TestCase;
include 'authentication.php';
class authenticationTest extends TestCase
{
    public function testAuthentication()
    {
    //your test here, call LoginMe with expected parameters
    }
}

您可能也会对此感兴趣:PHP Testing, for Procedural Code

【讨论】:

  • 我试过了,不行。我也在使用Linux,但在本地也不起作用。例如 linux 错误:PHP Fatal error: Class 'PHPUnit\Framework\TestCase' not found in /home/valkas1/public_html/testing.php on line 5
  • 我只是不明白我错过了什么
猜你喜欢
  • 1970-01-01
  • 2020-03-21
  • 2015-02-20
  • 1970-01-01
  • 1970-01-01
  • 2022-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多