kenjis / ci-phpunit-test

An easier way to use PHPUnit with CodeIgniter 3.x.
http://kenjis.github.io/ci-phpunit-test/
MIT License
586 stars 195 forks source link

Loading faker #110

Closed jamipietila closed 7 years ago

jamipietila commented 8 years ago

Hi, I've been using your package for a while and it rocks! I was wondering where should I load Faker library (composer) globally to use with all of the tests? For some reason every initialization of the library seems to take 5 seconds, so it adds up pretty quickly ($this->faker = Faker\Factory::create()).

Any ideas welcome!

kenjis commented 8 years ago

If you want to create Faker instance only once, you can set it in a static property of the TestCase class. See https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/master/application/tests/TestCase.php#L11

jamipietila commented 8 years ago

Thanks! That saves me 2 minutes from total execution time. Now little over 2 minutes.

Any ideas why controllers what use libraries outside Codeigniter are so slow to test? Example my registration controllers sends email notification using Swiftmailer, that takes over 20 seconds.

Same thing when tests use libraries like faker, guzzle etc. Every library adds around 5 seconds to test execution time.

kenjis commented 8 years ago

If you send email, it takes time. If you send HTTP requests with Guzzle, it also takes time. If you access your database, it takes time. But I don't know it is normal or not that taking over 20 seconds to send email.

If you don't need to test sending email, you can mock it and make your test faster.

jamipietila commented 8 years ago

Thanks for the reply!

Mocking is good way too, but I like to test emails with real smtp server (Swiftmailer -> Mailcatcher -> Guzzle), and that or course takes some time.

I just tested and looks like just initializing Faker or Guzzle adds 5 seconds both to test execution time (static property of the TestCase class).

So although I'm saving 2 minutes on total execution time when I run all tests, I'm adding 10 seconds to any single test even if I don't use those :D I guess I need to figure out another way to use those. Any ideas welcome!

kenjis commented 8 years ago

but I like to test emails with real smtp server

It depends on what you want to test. Mocking can't test real server.

I just tested and looks like just initializing Faker or Guzzle adds 5 seconds both to test execution time (static property of the TestCase class).

If you say it takes much longer only when testing, I don't know why.

I'm adding 10 seconds to any single test even if I don't use those

Check if they exist already. So you initialize them only once during test execution.