def wait_for
Timeout.timeout(Capybara.default_wait_time) do
loop until
begin
yield
rescue MiniTest::Assertion
end
end
yield
end
Showing posts with label testing. Show all posts
Showing posts with label testing. Show all posts
Wait for Javascript and AJAX changes in Capybara
Recently I was having trouble with JS-enabled tests with Capybara on a Ruby on Rails app, when clicking stuff around and then asserting about content that was not on the page yet. I tried several solutions I found online, but either they were talking about wait_until, which is a method that's not available in Capybara anymore, or they just didn't work for me. Therefore, I decided to roll my own:
Labels:
capybara,
Ruby on Rails,
testing
Replay network requests on your tests: VCR.py
Today I'm happy to present you another library to aid with testing, this time related to tests that need network conectivity: VCR.py
VCR.py, a Python version of Ruby's VCR, works by recording the HTTP requests and responses your test makes, and then replays them when the tests is run again, thus achieving network independence. As a side effect, it also makes the test much faster, which is always nice.
Using it on a typical test is very easy, you just need to import it, and then surround the test that will interact with the network with a with vcr.cassete() block:
VCR.py, a Python version of Ruby's VCR, works by recording the HTTP requests and responses your test makes, and then replays them when the tests is run again, thus achieving network independence. As a side effect, it also makes the test much faster, which is always nice.
Using it on a typical test is very easy, you just need to import it, and then surround the test that will interact with the network with a with vcr.cassete() block:
Freezegun, time travel for Python tests
Yesterday I discovered freezegun, a Python library to run test cases at defined moments in time. It feels just like the equivalent of Ruby's timecop gem, and it saves me a lot of headaches trying to mock the datetime module.
What I was trying to test is a method named delete_expired_promises, which removes reservations for items after the expiration time has run out. The code, using freezegun came to be this simple:
As you can see, I "freeze" the time at different intervals, create reservations, then run the delete_expired_promises method, and check if they have been removed correctly. It could not be more straighforward!
What I was trying to test is a method named delete_expired_promises, which removes reservations for items after the expiration time has run out. The code, using freezegun came to be this simple:
with freeze_time('2013-10-30 10:00:00'):
self.store.save('1111', '2222', reservation_id='1')
self.store.save('3333', '4444', reservation_id='2')
with freeze_time('2013-10-30 11:00:00'):
self.store.save('1111', '2222', reservation_id='3')
with freeze_time('2013-10-30 12:30:00'):
self.store.delete_expired_promises()
self.assertIsNone(self.collection.find_one({'reservation_id': '1'}) )
self.assertIsNone(self.collection.find_one({'reservation_id': '2'}) )
self.assertIsNotNone(self.collection.find_one({'reservation_id': '3'}) )
As you can see, I "freeze" the time at different intervals, create reservations, then run the delete_expired_promises method, and check if they have been removed correctly. It could not be more straighforward!
131% faster Ruby tests, no new code needed
Some speed improvements on a Rails app test suite, about 100 assertions.
Initial state – Ruby 1.9.3-p448:
Total run time: 37s
First improvement – Ruby 1.9.3-p448 with Railsexpress patches:
Tuning your Ruby GC for fun and profit
By simply adding the following to my .zshrc:
export RUBY_HEAP_MIN_SLOTS=2000000
export RUBY_HEAP_SLOTS_INCREMENT=500000
export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1
export RUBY_GC_MALLOC_LIMIT=70000000
export RUBY_FREE_MIN=100000
I'm getting a 10% increase running the test suite on a Rails app:
# Before:
rake 24.57s user 3.77s system 95% cpu 29.690 total
# After:
rake 21.41s user 4.22s system 95% cpu 26.897 total
Try it out and see it for yourself :)
jQuery plugin for better HTML file inputs
Have you ever wanted to style an HTML file input in a easy way? I did, so I took this guide:
quirksmode.org/dom/inputfile.html
quirksmode.org/dom/inputfile.html
Also, here are some slides I made for colognejs.de about developing a a jQuery plugin with Jasmine specs, using this plugin as an example:
Hope you like it!
Labels:
Jasmine,
Javascript,
jQuery,
Presentation,
Talk,
testing
Subscribe to:
Posts (Atom)

