简体   繁体   English

使用Codeception运行现有的Laravel 5.1 PHPUnit测试

[英]Using Codeception to run existing Laravel 5.1 PHPUnit tests

I have a number of tests written for a Laravel 5.1 application, and I'm currently using PHPUnit to run the tests. 我为Laravel 5.1应用程序编写了许多测试,我目前正在使用PHPUnit来运行测试。 I'm attempting to investigate codeception as we have other applications that aren't built on Laravel, so codeception looks like the easiest way to get a similar interface for testing. 我正在尝试研究代码,因为我们有其他应用程序不是基于Laravel构建的,所以代码看起来是获得类似测试接口的最简单方法。

We have a reasonably large team, so I would prefer to use a single consistent command for testing across all projects instead of some using codecept and some using phpunit . 我们有一个相当大的团队,所以我更喜欢使用单个一致的命令来测试所有项目,而不是使用codecept和一些使用phpunit

Here's what I've tried as my codeception.yml : 这是我作为我的codeception.yml尝试的:

actor: Tester
paths:
   tests: tests
   log: storage/codeception/_output
   data: storage/codeception/_data
   support: storage/codeception/_support
   envs: storage/codeception/_envs

modules:
   enabled:
       - Laravel5:
           environment_file: .env

But I get this response: 但我得到了这样的答复:

 $ codecept run Codeception PHP Testing Framework v2.1.4 Powered by PHPUnit 4.8.18 by Sebastian Bergmann and contributors. [RuntimeException] Suite '' could not be found 

So my question is this: 所以我的问题是:

How do I convince codeception to run my existing PHPUnit tests with as little modification as possible? 如何通过尽可能少的修改来说服代码运行我现有的PHPUnit测试?

After a lot of time, I found out that it's possible, but you have to make some modifications to how your tests are laid out. 经过很长一段时间后,我发现它是可能的,但你必须对你的测试布局做一些修改。 The bonus, though, is that it doesn't stop you from just running PHPUnit itself. 但是,奖励是它不会阻止你只运行PHPUnit本身。

For posterity, here's how to do it: 对于后代,这是如何做到的:

  1. Move your unit tests and TestCase.php into a folder named unit under the tests folder. 将您的单元测试和TestCase.php移动到tests文件夹下名为unit的文件夹中。

It should look like this: 它应该如下所示:

tests/
tests/unit/
tests/unit/MyUnitTest.php
tests/unit/TestCase.php
  1. Add the unit.suite.yml file in the tests/ folder. tests/文件夹中添加unit.suite.yml文件。

The contents should look something like this: 内容应如下所示:

modules:
    enabled:
        - Laravel5:
            environment_file: .env.testing
  1. Update your composer.json to with the correct folder for the testing classmap 更新您的composer.json到与测试类映射正确的文件夹

In your autoload-dev section: 在您的autoload-dev部分中:

"classmap": [
    "tests/unit/TestCase.php"
]
  1. Update your TestCase.php to load bootstrap.php from the correct folder. 更新TestCase.php以从正确的文件夹加载bootstrap.php

This should be in the createApplication() method at the top of TestCase.php 这应该位于TestCase.php顶部的createApplication()方法中

public function createApplication()
{
    // $app = require __DIR__ . '/../bootstrap/app.php';
    $app = require __DIR__ . '/../../bootstrap/app.php';

    $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

    return $app;
}

Finally, run composer dump-autoload , and codeception should run. 最后,运行composer dump-autoload ,并运行代码。 As a bonus, phpunit should also still run. 作为奖励, phpunit还应该运行。

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

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