2020-10-21

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

  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哭了)
那位大佬遇到过这样的问题吗?能不能指点一下

https://q.cnblogs.com/q/126363/

发表回复

Your email address will not be published. Required fields are marked *