mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make test logs easier to read.
Tagging every message in tests makes the logs really wide. It's great for grepping, but annoying to open in an editor or a narrow terminal. Try out a different approach: spit out a heading before each test.
This commit is contained in:
parent
42d9b48012
commit
b82109495b
2 changed files with 11 additions and 13 deletions
|
@ -1,26 +1,25 @@
|
|||
module ActiveSupport
|
||||
module Testing
|
||||
module TaggedLogging
|
||||
# Logs a "PostsControllerTest: test name" heading before each test to
|
||||
# make test.log easier to search and follow along with.
|
||||
module TaggedLogging #:nodoc:
|
||||
attr_writer :tagged_logger
|
||||
|
||||
def before_setup
|
||||
tagged_logger.push_tags(self.class.name, __name__) if tagged_logging?
|
||||
if tagged_logger
|
||||
heading = "#{self.class}: #{__name__}"
|
||||
divider = '-' * heading.size
|
||||
tagged_logger.info divider
|
||||
tagged_logger.info heading
|
||||
tagged_logger.info divider
|
||||
end
|
||||
super
|
||||
end
|
||||
|
||||
def after_teardown
|
||||
super
|
||||
tagged_logger.pop_tags(2) if tagged_logging?
|
||||
end
|
||||
|
||||
private
|
||||
def tagged_logger
|
||||
@tagged_logger ||= (defined?(Rails.logger) && Rails.logger)
|
||||
end
|
||||
|
||||
def tagged_logging?
|
||||
tagged_logger && tagged_logger.respond_to?(:push_tags)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -182,7 +182,6 @@ class TestCaseTaggedLoggingTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
def test_logs_tagged_with_current_test_case
|
||||
tagged_logger.info 'test'
|
||||
assert_equal "[#{self.class.name}] [#{__name__}] test\n", @out.string
|
||||
assert_match "#{self.class}: #{__name__}\n", @out.string
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue