简体   繁体   English

Symfony2 RESTful应用程序-测试

[英]Symfony2 RESTful application - tests

Which way is better to do tests in Symfony2 RESTful application or in any others applications. 在Symfony2 RESTful应用程序或任何其他应用程序中进行测试的哪种方法更好。

Example: I want to test PostController , which contains actions: getPostsAction() , getPostAction() , putPostAction() , postPostAction() , deletePostAction() . 示例:我要测试PostController ,其中包含以下动作: getPostsAction()getPostAction()putPostAction()postPostAction()deletePostAction()

So I've created class PostControllerTest . 所以我创建了class PostControllerTest

Question is: 问题是:

a) Should I create individual test for each action? a)我应该为每个动作创建单独的测试吗? Ex - testing updating: 防爆测试更新:

  • first load posts from fixture 夹具上的第一个负载柱
  • get id of one of this added posts 获取此添加帖子之一的ID
  • update this post through PostController@putPostAction 通过PostController@putPostAction更新此帖子
  • check updating result with entity manager 与实体管理器检查更新结果

b) Or test few actions in one test(every through controller)? b)或在一个测试中测试几个动作(每个都通过控制器)? Ex: 例如:

  • first test creating 第一次测试创建
  • next listing(expecting added post) 下一个列表(期待添加的帖子)
  • updating 更新
  • listing(expecting updated post) 上市(期待更新的帖子)
  • deleting 删除
  • listing(expecting 0 rows). 上市(预期0行)。

I think first option is OK, because I test only one controller action in one single test. 我认为第一个选择是可以的,因为我在一个测试中仅测试了一个控制器动作。 I exactly know what I want to test. 我完全知道我要测试的内容。 But in second case I can do tests faster and don't need use fixtures and entity manager. 但是在第二种情况下,我可以更快地进行测试,不需要使用夹具和实体管理器。

Which way will be better and why? 哪种方法会更好,为什么呢?

I cannot say for sure what's the best solution for you but I think a good idea is to make it more functional as you said. 我不能肯定地说什么是最适合您的解决方案,但我认为一个好主意是使它更像您所说的那样起作用。 You can make your tests to follow a flow, like: 您可以使测试遵循流程,例如:

  • Create user 创建用户
  • Update user 更新用户
  • Delete user 删除用户

Making a complete cycle without leaving trash data. 进行完整的循环,而不会留下垃圾数据。

I have an opensource project which I do exactly like this, and I also use some traits to help me on creating objects and validating responses from my Rest calls. 我有一个完全像这样的开源项目,并且我还使用一些特性来帮助我创建对象和验证来自Rest调用的响应。 You can see a real test in here: https://github.com/renatomefidf/sammui/blob/master/src/Renatomefi/TranslateBundle/Tests/Controller/ManageControllerTest.php 您可以在这里看到真实的测试: https : //github.com/renatomefidf/sammui/blob/master/src/Renatomefi/TranslateBundle/Tests/Controller/ManageControllerTest.php

By using the @depends annotation from phpunit you can determine the flow and make sure you have a logical path for your tests. 通过使用phpunit中的@depends批注,您可以确定流程并确保您具有用于测试的逻辑路径。

I hope this helps you, and if you have more questions let me know! 希望对您有所帮助,如果您还有其他问题,请告诉我!

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

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