首页 新闻 会员 周边 捐助

PHPunit:测试类里面调用其他类,命令行类一直提示找不到类

0
悬赏园豆:100 [待解决问题]
  1. 目录

    controller
        login.php
        loginController.php
    tests
        loginTest.php

  1. 代码详情:

    1. login
    <?php
    
    class login
    {
        // 测试工具PHPUnit要求一定要在这里给变量默认值,于是默认为空。
        public function login($Sname = "",$Sage = "",$tableName = "") {
            $con = mysqli_connect("localhost", "root", "root", "yoga_io");
            if (!$con) {
                die('连接失败:' . mysqli_error());
            } else {
                mysqli_query($con, "SET NAMES utf8");
    
                $result = mysqli_query($con, "SELECT * FROM $tableName where Sname = $Sname and Sage = $Sage");
                if (!$result || mysqli_num_rows($result) == 0) {
                    return "false";
                } else {
                    $result_arr = mysqli_fetch_assoc($result);
                    return json_encode($result_arr, JSON_UNESCAPED_UNICODE);
                }
            }
        }
    }
    1. loginTest

      <?php
      use PHPUnit\Framework\TestCase;
      
      class loginTest extends TestCase
      {
          public function testLoginSuccess() {
              $expected = '{"Sid":"1","Sname":"张三","Sage":"40","Sswx":"男"}';
      
              $Sname = '张三';
              $Sage = '40';
              $tableName = 'teacher';
              $lg = new Login();
              $actual = $lg->login($Sname,$Sage,$tableName);
      
              $this->assertEquals($expected,$actual);
          }
      
          function testLoginFail() {
              $expected = 'false';
      
              $Sname = '2';
              $Sage = '56';
              $tableName = 'teacher';
      
              $lg = new Login();
              $actual = $lg->login($Sname,$Sage,$tableName);
              $this->assertEquals($expected,$actual);
          }
      }
  2. 错误信息:

PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

Warning:       Invocation with class name is deprecated

EE                                                                  2 / 2 (100%)

Time: 515 ms, Memory: 4.00 MB

There were 2 errors:

1) loginTest::testLoginSuccess
Error: Class 'Login' not found

C:\Users\Administrator\PhpstormProjects\untitled1\tests\loginTest.php:12

2) loginTest::testLoginFail
Error: Class 'Login' not found

C:\Users\Administrator\PhpstormProjects\untitled1\tests\loginTest.php:25

ERRORS!
Tests: 2, Assertions: 0, Errors: 2.

描述:就是不管换了多少个类做测试就是无法在测试类里面实例化其它类(T T哭了)
那位大佬遇到过这样的问题吗?能不能指点一下

不停1687的主页 不停1687 | 初学一级 | 园豆:102
提问于:2020-03-21 16:24
< >
分享
所有回答(1)
0

你在test里的login要require或者use你的Login类试试

风行天下12 | 园豆:3867 (老鸟四级) | 2020-03-21 16:46

好!!!!我试试!!!

支持(0) 反对(0) 不停1687 | 园豆:102 (初学一级) | 2020-03-21 17:27

嗯!可以了!谢谢

支持(0) 反对(0) 不停1687 | 园豆:102 (初学一级) | 2020-03-21 17:36

@不停1687: 不客气

支持(0) 反对(0) 风行天下12 | 园豆:3867 (老鸟四级) | 2020-03-21 18:10

@不停1687: 如果满意,给个采纳哈

支持(0) 反对(0) 风行天下12 | 园豆:3867 (老鸟四级) | 2020-03-24 14:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册