前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自动化测试:六个值得参考的 Laravel 开源项目

自动化测试:六个值得参考的 Laravel 开源项目

作者头像
顾翔
发布2019-12-12 11:20:06
1.9K0
发布2019-12-12 11:20:06
举报
文章被收录于专栏:啄木鸟软件测试

顾翔老师开发的bugreport2script开源了,希望大家多提建议。文件在https://github.com/xianggu625/bug2testscript,

主文件是:zentao.py 。bugreport是禅道,script是python3+selenium 3,按照规则在禅道上书写的bugreport可由zentao.py程序生成py测试脚本。

来源:http://www.51testing.com

最近我对自动化测试越来越感兴趣 —— 密切关注着 PHPUnit,TDD,Laravel Dusk,Travis 以及其他测试相关的关键词,所以我决定看看周围其他人是如何做自动化测试的。我在 GitHub 上找到了相当多的 Laravel 开源项目,从里面挑选出了 6 个来查看他们的测试方案。让我们分别看一下。

  免责声明: 我只是查看了完整的基于 Laravel 5.3+ 的 Laravel 项目(不包括依赖包)

 1. Laravel.io portal

  URL: https://github.com/laravelio/...

  最近重新启动的 Laravel.io 已经将代码在 GitHub 上开源。Dries Vints 在这个项目中写的测试非常好。

  Laravel.io 使用功能测试 (Feature testing) 和组件测试 (Component testing)(和单元测试差不多)。有趣的是,在这两种测试中都进行了相同或相似的测试。

例子 1 -- tests/Feature/ReplyTest.php

public function users_can_add_a_reply_to_a_thread()  {  factory(Thread::class)->create(['subject' => 'The first thread', 'slug' => 'the-first-thread']);  $this->login();  $this->visit('/forum/the-first-thread')  ->type('The first reply', 'body')  ->press('Reply')  ->see('The first thread')  ->see('The first reply')  ->see('Reply successfully added!');  }

 例子 2 -- tests/Components/Jobs/CreateReplyTest.php

public function we_can_create_a_reply()  {  $job = new CreateReply('Foo', '', $this->createUser(), factory(Thread::class)->create());  $this->assertInstanceOf(Reply::class, $job->handle());  }

  这样做很好: 同时测试 Jobs 层和实际在浏览器中点击一些东西。

  我还注意到 Laravel.io 已经升级到了 Laravel 5.4, 但是测试套件仍然使用的是5.3的风格, 使用 BrowserKitTestCase implementation。 这没有什么问题,仅仅是一个提醒。

  这个项目也使用了 Travis 进行持续集成, 后来我发现大多数项目都使用了它。

 2. Cachet —— 一个开源状态页面系统

  URL: https://github.com/cachethq/C...

  在 James Brooks 和 Graham Campbell 的带领下,这个项目有一个庞大的测试组件。他甚至通过观察表层很难理解。

  所以,我们从哪里开始... 事实上,我甚至不会深度燕郊这个项目的测试逻辑, 因为他太难理解了,这是一个例子 —— tests/Models/ComponentTest.php:

use AltThree\TestBench\ValidationTrait;  use CachetHQ\Cachet\Models\Component;  use CachetHQ\Tests\Cachet\AbstractTestCase;  class ComponentTest extends AbstractTestCase  {  use ValidationTrait;  public function testValidation()  {  $this->checkRules(new Component());  }  }

  好吧,这里用到了 ValidationTrait,然后是一些 AbstractTestCase。同时这段逻辑是所有的测试 —— 一些抽象的 "魔术" 正在执行所有的工作。

  我不是说这是坏事 —— 十分确定他在内在的东西里工作的很好。他只是不容易先学习和遵循。但如果有人想深入研究 —— 祝好运!

 3. October CMS

  URL: https://github.com/octobercms...

  市场上第一款基于 Laravel 的 CMS,他拥有非常不错的测试组件。

  首先 -—— tests 文件夹有一个 真正信息详实的 readme.md 文件,专门用于测试过程。

  October CMS 的所有测试包括:

  单元测试

  功能测试

  插件测试

  每个 "区域" 都有对应的基类来扩展 —— 有 TestCase,UiTestCase 和 PluginTestCase。

  逻辑也非常复杂和抽象 —— 这里有一个例子 tests/unit/backend/models/ExportModelTest.php:

代码语言:javascript
复制

  class ExportModelTest extends TestCase

  {

  //

  // 辅助

  //

  protected static function callProtectedMethod($object, $name, $params = [])

  {

  $className = get_class($object);

  $class = new ReflectionClass($className);

  $method = $class->getMethod($name);

  $method->setAccessible(true);

  return $method->invokeArgs($object, $params);

  }

  //

  // 测试

  //

  public function testEncodeArrayValue()

  {

  $model = new ExampleExportModel;

  $data = ['foo', 'bar'];

  $result = self::callProtectedMethod($model, 'encodeArrayValue', [$data]);

  $this->assertEquals('foo|bar', $result);

  $data = ['dps | heals | tank', 'paladin', 'berserker', 'gunner'];

  $result = self::callProtectedMethod($model, 'encodeArrayValue', [$data]);

  $this->assertEquals('dps \| heals \| tank|paladin|berserker|gunner', $result);

  $data = ['art direction', 'roman empire', 'sci-fi'];

  $result = self::callProtectedMethod($model, 'encodeArrayValue', [$data, '-']);

  $this->assertEquals('art direction-roman empire-sci\-fi', $result);

  }

  }

  如你所见,这里有一个静态辅助方法(顺便说一下,在其他类中重复使用),然后获取类/方法并调用他啊, 我确信作者能立即理解逻辑,但这对外人来说很困难。

  同样有趣的是,OctoberCMS 使用 Selenium 来获取一些功能:tests/readme.md 文件提到了设置文档。

 4. Orgmanager —— GitHub 组织的邀请系统

  URL: https://github.com/orgmanager...

  这是 Miguel Piedrafita 的一个非常简单的项目,Orgmanager 的测试也是非常简单易懂的。还分为单元,功能和 API 测试。

  我在这里看到一个有趣的示例 —— 从测试中调用 Artisan 命令,例如 unit/JoinTest.php:

代码语言:javascript
复制

   public function testJoinCommand()

  {

  $user = factory(User::class)->create();

  $org = factory(Org::class)->create([

  'userid' => $user->id,

  ]);

  Github::shouldReceive('authenticate')

  ->once()

  ->with($org->user->token, null, 'http_token')

  ->andReturn();

  Artisan::call('orgmanager:joinorg', [

  'org'      => $org->id,

  'username' => $user->github_username,

  ]);

  $this->assertEquals($user->github_username.' was invited to '.$org->name."\n", Artisan::output());

  }

  调用 artisan 命令并断言其输出 —— 非常有趣。我确定他有效,但这是非标准的方式。

 5. PHPMap

  URL: https://github.com/PHPMap/phpmap

  由 Florian Wartner 创建及维护。

 PHPMap 有一个测试组件,使人联想到 Laracasts 或 测试驱动 Laravel 课程 讲述的标准。这是 Feature/FavoritesTest.php 的例子。

代码语言:javascript
复制

  public function guests_can_not_favorite_anything()

  {

  $this->withExceptionHandling()

  ->post('forum/replies/1/favorites')

  ->assertRedirect('/login');

  }

  public function an_authenticated_user_can_favorite_any_reply()

  {

  $this->signIn();

  $reply = create('App\Models\Forum\Reply');

  $this->post('forum/replies/'.$reply->id.'/forum/favorites');

  $this->assertCount(1, $reply->favorites);

  }

  PHPMap 的测试分为单元,功能及 Laravel Dusk 等等!最后我发现了一个真正在生产环境使用 Dusk 的项目。这是他的门面 —— tests/Browser/MapTest.php:

代码语言:javascript
复制

  public function testMap()

  {

  $this->browse(function ($browser) {

  $browser->visit('/map')

  ->assertSee('PHPMap');

  });

  }

6. Timegrid —— 免费,开源,在线操作平台

  URL: https://github.com/timegridio...

  Timegrid 的最大贡献者是 Ariel Vallese,同时他在测试方面做了非常好的工作。

 这里只有很多的测试: 单元,验收和集成,每个文件都有更深的子文件夹目录,例如:

—— acceptance/scenarios/consulting/ConsultingScenarioTest.php:

代码语言:javascript
复制

   public function it_fits_for_consulting_scenario()

  {

  $this->arrangeScenario();

  $this->the_business_publishes_a_consulting_service();

  $this->the_business_publishes_vacancies();

  $this->a_user_subscribes_to_business();

  $this->the_user_queries_vacancies();

  $this->it_provides_available_times_for_requested_service_date();

  $this->the_user_takes_a_reservation();

  $this->the_user_sees_the_reservation_ticket();

  }

  public function the_business_publishes_a_consulting_service()

  {

  $this->service = $this->makeService([

  'name'     => 'OnSite 4hs Support',

  'duration' => 60 * 4,

  ]);

  $this->actingAs($this->owner);

  $this->call('POST', route('manager.business.service.store', $this->business), $this->service->toArray());

  $this->assertCount(1, $this->business->fresh()->services);

  }

  一个一体化的方法,之后是一个个列举更多的测试:

  仓库中的官方统计数据看起来非常好: 89% 的测试覆盖率。

  最后,有趣的是,作者甚至测试了迁移文件,如 tests/unit/migration/MigrationTest.php:

代码语言:javascript
复制

   public function it_refreshes_rollbacks_and_seeds_the_database()

  {

  $database = env('DB_CONNECTION');

  $this->assertNotNull($database);

  $exitCode = Artisan::call('migrate:refresh', ['--database' => $database]);

  $this->assertEquals(0, $exitCode);

  $exitCode = Artisan::call('migrate:rollback', ['--database' => $database]);

  $this->assertEquals(0, $exitCode);

  $exitCode = Artisan::call('migrate', ['--database' => $database]);

  $this->assertEquals(0, $exitCode);

  $exitCode = Artisan::call('db:seed', ['--database' => $database]);

  $this->assertEquals(0, $exitCode);

  }

  在测试中使用 Artisan 命令或许不是最佳的设计模式,但他只是测试任何 web 应用中最重要的功能之一。

 总体结论

  在看过所有这些不同的项目之后(以及由于各种原因未提及的),以下是我对自己关于测试的主要要求:

  不在 单元 "或" 功能 中做选择, —— 大多数项目两者兼具,或者更多类型的测试;

  大多数项目使用持续集成(通常是 Travis)和测试组件 —— 否则,为什么反感写测试呢?

  这里有非常多的不同方式构建测试 —— 这完全取决于项目,这里没有“高招”;

  还有很多方法对内部测试功能分组 —— 辅助方法,抽象类,种子数据等。没有具体规则,找准适用于你的内容。

  迁移到较新版本的 Laravel 可能很痛苦 —— 例如,5.3 版本的测试看上去和 5.4 版本不一样。所以你需要提前考虑更新。

  从不同角度考虑 —— 当你的项目成长起来,你将不得不回看及修改/添加测试。在这些项目中,我”预感“有一些遗留代码,只是因为有些测试将不在被使用。

  以上是我的经验,有没有你要添加到开源项目列表中来学习测试的内容?

星云测试

http://www.teststars.cc

奇林软件

http://www.kylinpet.com

联合通测

http://www.quicktesting.net

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-04-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
持续集成
CODING 持续集成(CODING Continuous Integration,CODING-CI)全面兼容 Jenkins 的持续集成服务,支持 Java、Python、NodeJS 等所有主流语言,并且支持 Docker 镜像的构建。图形化编排,高配集群多 Job 并行构建全面提速您的构建任务。支持主流的 Git 代码仓库,包括 CODING 代码托管、GitHub、GitLab 等。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档