简体   繁体   English

代码接收单元测试

[英]Codeception unit test

I'm new to PHP and Codeception and the whole concept of unit testing.我是 PHP 和 Codeception 以及单元测试的整个概念的新手。 So far I've followed Codeception's quick start guide ( http://codeception.com/quickstart ) and read their documentation ( http://codeception.com/docs/05-UnitTests ).到目前为止,我一直在遵循 Codeception 的快速入门指南 ( http://codeception.com/quickstart ) 并阅读他们的文档 ( http://codeception.com/docs/05-UnitTests )。

I have managed to set up the testing environment, and我已经设法建立了测试环境,并且

  1. Created the unit test file ( php codecept.phar generate:test unit ExampleTest )创建单元测试文件( php codecept.phar generate:test unit ExampleTest
  2. Run the test command ( php codecept.phar run unit ExampleTest ), which returns an error:运行测试命令( php codecept.phar run unit ExampleTest ),返回错误:

There was 1 error:有 1 个错误:

 1) ExampleTest: Validation Test tests\\unit\\ExampleTest.php:testValidation [Error] Class 'User' not found #1 ExampleTest->testValidation #2 C:\\laragon\\www\\kario\\vendor\\bin\\codecept.phar:5

What I don't understand is how does the test file know which PHP file to run the test on?我不明白的是测试文件如何知道在哪个 PHP 文件上运行测试?

My laragon project is named kario , and sits in C:\\laragon\\www\\kario\\resources\\views\\pages\\orders while the test unit file is in C:\\laragon\\www\\kario\\vendor\\bin\\tests\\unit\u003c/code> .我的 laragon 项目名为kario ,位于C:\\laragon\\www\\kario\\resources\\views\\pages\\orders而测试单元文件位于C:\\laragon\\www\\kario\\vendor\\bin\\tests\\unit\u003c/code> 。

I had this question too and found the answer for myself.我也有这个问题,并为自己找到了答案。 Posted here http://phptest.club/t/beginner-codeception-unit-test-help/1849 but also, here you go:在这里发布http://phptest.club/t/beginner-codeception-unit-test-help/1849而且,你去吧:

First, some details.首先,一些细节。 I am using Codeception v2.4.1, powered by PHPUnit 7.1.4.我正在使用由 PHPUnit 7.1.4 提供支持的 Codeception v2.4.1。 The answer is:答案是:

In codeception.yml , add these two lines:codeception.yml ,添加以下两行:

    settings:
        bootstrap: _bootstrap.php

Here _bootstrap.php can be whatever you want the name of your bootstrap file to be.这里_bootstrap.php可以是任何你想要的引导文件的名称。

You must place _bootstrap.php in each of the following directories as follows:您必须将_bootstrap.php放在以下每个目录中,如下所示:

tests/unit/_bootstrap.php
tests/functional/_bootstrap.pp
tests/acceptance/_bootstrap.php

In my tests/unit/_bootstrap.php file, I placed the following code:在我的tests/unit/_bootstrap.php文件中,我放置了以下代码:

<?php
use Codeception\Util\Autoload;
Autoload::addNamespace('myclassnamespace', __DIR__ . '/../../Classes/');

To make sure I had the right path to Classes, I used trigger_error(__DIR__) in my _bootstrap.php before I added the Autoload line.为了确保我有正确的类路径,我在添加Autoload行之前在_bootstrap.php使用了trigger_error(__DIR__)

Then in my tests/unit/TestAddCest.php , I placed the following line at the beginning of the file:然后在我的tests/unit/TestAddCest.php ,我tests/unit/TestAddCest.php放在文件的开头:

<?php
use mynamespace;

And in my test function looks like this (note the instantiation of the User class):在我的测试函数中看起来像这样(注意 User 类的实例化):

    public function tryToTest(UnitTester $I)
    {
      $user = new mynamespace\User('someusername');
      $I->assertEquals('someusername', $user->username);
    }

I hand typed that function because I'm not on the same machine with the code and didn't feel like getting it over, so it may have a typo or bug, but you get the idea.我手动输入了该函数,因为我与代码不在同一台机器上并且不想完成它,所以它可能有拼写错误或错误,但你明白了。

Edit 05/01/2018: someone else answered me on http://phptest.club :编辑 05/01/2018:有人在http://phptest.club上回答了我:

It would be better to configure autoloading of your classes in composer.json and let Composer to do the rest, unless you try to avoid using Composer.最好在composer.json配置类的自动加载并让 Composer 来完成剩下的工作,除非您试图避免使用 Composer。

https://getcomposer.org/doc/01-basic-usage.md#autoloading https://getcomposer.org/doc/01-basic-usage.md#autoloading

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM