Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outputting (puts, print) in Rails Unit Tests

How come commands like puts and print don't show up in the console when running ActiveSupport::TestCase tests?

Makes it very hard to debug if I can't outputs some inspections in a couple of methods.

Thanks!

like image 712
Alexandre Avatar asked Oct 28 '09 19:10

Alexandre


4 Answers

You can use the rails logger to see your output:

Rails::logger.debug "Interesting stuff"

Run tail -f log/test.log on the command line (from the project's root in a separate Terminal tab or window) to see the results.

like image 131
Baldu Avatar answered Oct 13 '22 16:10

Baldu


I've just been struggling with this under rails (3.2). I don't know how things have changed between versions but the answers don't actually answer the question. Rather using

$stdout.puts msg

outputs to the console along with the other console messages when running individual tests.

like image 44
Mr Morphe Avatar answered Oct 13 '22 16:10

Mr Morphe


I use puts all the time in tests when I'm quickly hacking debugging a single test. So I don't use rake test:*, rather run the individual test and the output shows up.

ruby -Itest test/unit/user/context_test.rb
like image 3
Krut Avatar answered Oct 13 '22 16:10

Krut


You can use puts in an individual test as follows eg:

puts "\n\n #{@object.name}"

This will look something like as follows in your terminal window as the tests run

Started E...EEE

United Kingdom .E

Finished in 2.787886 seconds.

(where @object.name == "United Kingdom" in this case)

It's quite a crude method but very quick for simple debugging

like image 2
Mitch Avatar answered Oct 13 '22 16:10

Mitch