Why are Test :: Unit tests running so slowly?

>rails -v Rails 1.2.6 >ruby -v ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32] 

When I run a test device (which checks the rail model class) like this, it takes 20-30 seconds to start these tests (show "Loaded set ..."). What gives?

 >ruby test\unit\category_test.rb require File.dirname(__FILE__) + '/../test_helper' class CategoryTest < Test::Unit::TestCase def setup Category.delete_all end def test_create obCategoryEntry = Category.new({:name=>'Apparel'}) assert obCategoryEntry.save, obCategoryEntry.errors.full_messages.join(', ') assert_equal 1, Category.count assert_not_nil Category.find(:all, :conditions=>"name='Apparel'") end #.. 1 more test here end 

This is Rails using the MySql database without any attachments. This time he ran 30 seconds + to run.

+1
source share
7 answers

Take a look at the Rails Test Server .

Quote from the author:

โ€œEach time you run the test in Rails applications, the whole environment is loaded, including libraries that are not changing between two consecutive runs. It can take a considerable amount of time. What if we could load the environment once and restart changing the details before each run? Introducing RailsTestServing.

With RailsTestServing runtime, one test file came out from 8 seconds to 0.2 seconds on my computer. This is an x40 speed improvement. Now I donโ€™t think twice before hitting โŒ˜R in TextMate. It feels liberation! "

(It was featured on the Rails Envy Podcast last week, where I found it.)

+3
source

When running any tests, Rails first loads any fixtures that you have (in test / fixtures) and recreates the database with them.

20-30 seconds sounds very slow. Do you have many tools that you need to download before running your tests, or is your database slow?

+2
source

The Ruby gem tool follows a path detection algorithm that is apparently not Windows (as I see from your ruby -v ).

You can get a clear image if you trace, for example, loading a Rails application using ProcMon . Each (I really mean each) require runs a scan across all directories in the Ruby path plus all gem directories. A typical require takes 20 ms on an average computer. Because Rails is hundreds of require s, those 20 ms are easy to add up to seconds every time you start the Rails framework. Take the time to initialize the fixtures in the database, and you will get a better idea of โ€‹โ€‹why it takes so long to start running test cases.

Perhaps due to each architecture and file system implementation (path caching, etc.) this is less of a problem on Linux than on Windows. However, I do not know who you are blaming. It looks like the NTFS file system can be improved with a better implementation of path caching, but obviously the gem tool can implement the caching itself and have performance that is not so platform dependent.

+1
source

Test :: Unit seems to be the easiest, but also one of the slowest ways to do unit testing with Ruby. One option: ZenTest .

0
source

The launch of the test block is not particularly slow, and nowhere about 20 seconds.

 (11:39) ~/tmp $ cat test_unit.rb require 'test/unit' class MyTest < Test::Unit::TestCase def test_test assert_equal("this", "that") end end (11:39) ~/tmp $ time ruby test_unit.rb Loaded suite test_unit Started F Finished in 0.007338 seconds. 1) Failure: test_test(MyTest) [test_unit.rb:4]: <"this"> expected but was <"that">. 1 tests, 1 assertions, 1 failures, 0 errors real 0m0.041s user 0m0.027s sys 0m0.012s 

This is probably what you do in your tests. Are you doing something complicated? Database setup? Getting something from the Internet?

0
source

What does your test_helper.rb look like? Do you use copy lights?

 self.use_instantiated_fixtures = true 

[edit]

If this parameter is set to true, set it to false.

0
source

A full shot in the dark, but most of the time I see a long run time on things, this is usually due to some kind of reverse DNS lookup happening with some TCP sockets somewhere along the way.

Try adding:

 require 'socket' Socket.do_not_reverse_lookup = true 

at the top of the test file after your other require line.

0
source

Source: https://habr.com/ru/post/916775/


All Articles