-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Testing] Wrappers over Session, Headers, Cookies #130
Comments
We basically need to create Session and Cookie components specifically for testing purpose. For headers, most likely we need to provide a Response component for testing (also need to clean up existing code that directly uses |
Yes, @qiangxue i will write in this issue tomorrow some common pitfalls that i faced with when developing integration for Yii 1.1. |
That will be cool. Thank you! |
Ok, so what basically need to be done. In the Codeception (similar to Behat+Mink) there is a 2 type of tests for web-interface:
There are two types of functional tests in Codeception: one can be run with web-server ("PhpBrowser" module), other can be preformed through symfony browser-kit component. As you see some basic support for Codeception in Yii2 to be done is to implement connector (Yii2 client) (symfony browser-kit client) and module (Yii2 codeception module) that inherits his behavior with common methods from
Now you also can use Yii1 So overall, all that need to be done for second type of functional Codeception tests is implementing client-connector and codeception module. Yii applications can be tested via Codeception even with "PhpBrowser" module (through curl) that requires server, but i think that integration through browser-kit would be very useful too. P.S. in following messages i will write some common pitfalls for sessions, headers, cookies. |
Session. Main thing for session was to avoid session_start() call because it was already started by Codeception (in php 5.3 even |
Request and Cookies. Main work to be done is here. Unfortunately Yii1 does not provide some response-object and dont mock |
Summary:
|
@Ragazzo Thank you very much for these writeup. I have been reading Codeception tutorials these days when having time. So far it looks great to me (the tutorials are also very well written BTW). From my understanding, we mainly need to provide integration support for functional testing, right? Do we need to do anything for unit testing? And as you wrote, for functional testing, we should provide at least provide connector and module for Codeception. We also need to provide additional application components specifically for testing purpose, including Request, CookieCollection, Response, and other things that help to reset the testing environments. |
yes, for second kind of functional codeception tests that run with symfony-browser-kit. (first "PhpBrowser" module is run with
Well, as i said Yii1 have good fixture manager and test-cases like Maybe also we can ask other developers like @schmunk42 or @jacmoe if they need some extra thing, but i think all Also, it would be nice i think to have 2 different modules, for those reasons:
Also maybe @DavertMik has some more ideas for unit-tets, dont know ;) Things that must be taken into considerations:
|
A Quick and dirty Fixtures Generator... Based upon the one from yii 1 TEST long comment removed. content is at #347 |
The @DavertMik For the in-memory session, is it used by functional tests or acceptance tests? Could file-based storage be used? We just need to clean up the session files after each test is done, and this would support multiple requests. |
For acceptance tests, file based storage should be used. With no alternatives. |
@DavertMik Thank you for your answer. Is there any requirement to clean up all session data? Since we have CacheSession which can make use of memory cache as storage, I think it should be fine for users to use it if speed is an issue. BTW, do you have any suggestion how we should redistribute codeception? I'm thinking to have deep integration with codeception, meaning we will develop tools (such as Gii) to generate test skeletons for codeception. Should we include codeception.phar in the Yii release, or make codeception a dependency of Yii? |
I think that there is no need to include phar file in release, all can be done with composer, or user can download file by himself. I think for |
Just for the record, I also experimented with the phar file and installation via composer. But I'd prefer using a global codeception installation, because installation via composer takes quite a while (without --prefer-dist) - or even better support both! |
@schmunk42 yes, i think those issues (with phar) were solved, can u check them again and if not - create issue on codeception repo? Also feel free to add some other suggestions if you want to ;) |
@Ragazzo Thanks for the info, works like a charm now! |
@qiangxue @schmunk42 I think Composer's version should be preferable to ship with Yii2. Currently we include too much dependencies within Codeception. If Yii2 will be installable via Composer it's better to try to include it as dependency (maybe a recommended to install). If Yii2 will be packaged as zip, a phar version can be included. Phar version requires manual upgrades, and has limitations in viewing and editing source code. So I think it is better for testers but developers will prefer composer's package instead. |
You're right, it couldn't be me who was arguing against composer ;) I'd say |
I have developed the Yii2 connector and module: Codeception/Codeception#387 I met some problems while developing the Yii2 connector and module:
@DavertMik and @Ragazzo: it would be great if you could shed some lights on my above questions. Thank you! |
Well, cool, i will do some brief look shortly) So about problems:
I think this can be solved by some workaround with
just pass them in connector like in Yii1, assigning to global
i think headers can be hold in the same way as in Yii1 connector, just pass them in 3 param, location header in Yii1 connector works fine.
well, need to think about this, @DavertMik ?
I also think that we still need 2 different modules: 1 - for functional tests (you did it now), 2 - for unit-tests and basic integration with Yii2, just to have ability to use Yii in Codeception for example to run unit-tests Yii with Codeception. For example, simple Yii1Basic helper that i use is very similar to Yii1 module - Codeception/Codeception#383 (comment). But need to consider that we do not need to include PhpUnit files twice, i wrote about that above, also will pay attention to this later in comments) Will also definitely make some review today-tomorrow and maybe say something more. Overall thanks for Yii2 support)) woot-woot :D Also would be cool to hear other Yii developers opinions. |
Things to be noticed, |
I also think, that to show users ability of UI-mapping and other we can make those Cept's in new Cest formats, will do this (there will be Cepts and Cest's just to show users the ability of the choice). Also i think it is better to put codeception tets under separated folder, i use it in Yii1 in subfolder |
@qiangxue What's about copying the |
Yep, a well known issue in Yii1. Even If you didn't use Codeception for testing, you will get this errors while testing session/auth things. The best workaround I've seen is:
But disabling
Yep, that's a good point.
No need to set
Yep, I have planned a post on Codeception internals. In 1.6.3 I refactored there a lot. So probably a time came to show how some things work and why. |
What if we want to remove some cookies?
That's how Yii2 is doing now. It will cause infinite redirections if $_POST is not set empty. I verified the status code was correct (302). Perhaps you can help review the code to see if I did something wrong.
Sorry, I read the code wrong. Actually I wanted to describe a different problem: if I modify
Could you explain a bit more about this? |
@qiangxue about cests (very similar to phpunit selenium testcase), will be good if in demo apps and boilerplates cepts and cests will be used just to show the ability of different formats. |
Yes, I read that before. Which format is the recommended one? I don't think we should provide both for demo purpose. We should mainly provide the best practice. |
Well, the answer is like always "It depends". If it is a very simple page and not so many actions to do on it, then simple plain Cept is enough, if it is a some CRUD or other actions that can be structured in separated blocks, then it is better to use Cest, because it allows to split some actions in protected methods, and also has |
There is no Actually, I think that Cest is better for cases where you need several similarly written test. For example, you want to check one form with different values. You create a Cest: <?php
protected function fillForm($I, $credit, $email)
{
$I->amOnPage('/purchase');
$I->fillField('Credit Card', $credit);
$I->fillField('Email', $credit);
$I->click('Order');
}
public function purchaseWithValidCard($I)
{
$this->fillForm($I, '1233 3453 2342 2343', 'jon@doe.com');
$I->see('Thank you for your order');
}
public function purchaseWithInvalidCard($I)
{
$this->fillForm($I, 'xxxxxx', 'jon@doe.com');
$I->see('Sorry, your crdit card is invalid');
} Cepts are better for more complex scenarios. If some parts scenario are common it's better to move them out to a PageObject or Controller class.
Sure. Do we have a demo app with some tests there? |
Not yet I think, but you could just install the advanced app and add some tests there. You may have a look at my posting in #389 for an app setup with additional extensions. Btw: I'd like to see examples and/or skeletons for all formats ... if possible gii templates would be great. |
Basic app has some. |
Thank you for explaining the cepts.:) @DavertMik You can try the basic app template: https://github.com/yiisoft/yii2/tree/master/apps/basic |
I know I've asked this before, but I still don't get the difference between functional and acceptance tests, why are they doing the same thing?! |
@qiangxue anything to be done on this one except documentation? |
I kept this open because we need to track down the last few comments in this thread. Will readdress these issues upon beta. |
Closed as we have addressed all issues in this ticket. |
In order to make application accept multiple requests in testing environment PHP
session_*
,header
,cookies
functions must not be called. Instead of creating real headers, sessions, and cookies, a fake inmemory storage should be used.Example:
This in memory storage should be easy to clean up.
I'm not aware of how implementation of this may look in Yii2. Probably there should be components for this. But the idea is to create wrappers and not allow this PHP functions to be called in testing env.
The text was updated successfully, but these errors were encountered: