gpt4 book ai didi

phpunit 测试和命名空间

转载 作者:行者123 更新时间:2023-12-04 17:06:48 27 4
gpt4 key购买 nike

我有

Error: Class 'app\Main' not found



我不知道如何解决它?

我使用 PHPSTORM 和 phpunit 最新版本。

Structure of my directories

主文件
<?php
namespace app;

class Main
{
public function Calc()
{
return 1;
}
}

主测试文件
<?php

namespace app;

use PHPUnit\Framework\TestCase;

class MainTest extends TestCase
{
public function testCalc()
{
$a = new Main();
}
}

phpunit.xml:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit verbose="true">
<testsuites>
<testsuite name="My Test Suite">
<directory suffix="Test.php" phpVersion="5.3.0" phpVersionOperator=">=">tests</directory>
<!-- <file phpVersion="5.3.0" phpVersionOperator=">=">/path/to/MyTest.php</file>-->
</testsuite>
</testsuites>
</phpunit>

Composer .json
{
"autoload": {
"classmap": [
"src/"
]
},
"require": {
"phpunit/phpunit": "7.0.1"
}
}

最佳答案

您需要告诉自动加载器在哪里可以找到类的 php 文件。这是通过遵循 PSR-0 标准完成的。

将此添加到您的 composer.json:

{
...
"autoload": {
"psr-0": { "": "tests/app/" }
}
}

现在自动加载器可以找到您需要让 PHPunit 知道在运行测试之前要执行的文件的类:引导文件。
但是,使用 PHPunit 配置文件会更好:
  <!-- /phpunit.xml.dist -->
<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="./vendor/autoload.php">

<testsuites>
<testsuite name="The project's test suite">
<directory>./tests/app</directory>
</testsuite>
</testsuites>

</phpunit>

关于phpunit 测试和命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48855035/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com