cro*_*don 6 ruby-on-rails testunit ruby-on-rails-3
Rails 3.2.1应用程序,使用minitest和autotest-rails宝石.
如果我运行"rake test",则输出为彩色.但是,如果我运行自动测试,输出不是彩色的.
使用自动测试时如何获得颜色输出?
这是我的test_helper.rb:
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'turn/autorun'
Turn.config do |c|
# use one of output formats:
# :outline - turn's original case/test outline mode [default]
# :progress - indicates progress with progress bar
# :dotted - test/unit's traditional dot-progress mode
# :pretty - new pretty reporter
# :marshal - dump output as YAML (normal run mode only)
# :cue - interactive testing
c.format = :pretty
# turn on invoke/execute tracing, enable full backtrace
c.trace = true
# use humanized test names (works only with :outline format)
c.natural = true
end
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
# Add more helper methods to be used by all tests here...
end
Run Code Online (Sandbox Code Playgroud)
如果我运行"rake test",则输出为彩色.
这是因为在自动测试中,运行测试进程的"终端"不是a tty,而当你直接运行时,它就是.
首先它是如何工作的,颜色代码在转义序列中定义,如果你把它们写下来就会看起来像\E[48mPRINT ME IN RED\E[0m(细节).
您的终端了解这些转义序列(通常),用颜色替换它们,改善输出的外观.
通过使用由终端模拟器中定义的环境变量,看它是它的输入,和输出流(即$stdin,$stdout和$stderr)processe(一个或多个)可确定颜色支持,以及是否它连接到一个终端(tty)或文件,或另一个过程,等等
当一个进程启动另一个进程时,您的进程,而不是终端是所有者,因此您的test输出不是与理解彩色转义序列的终端通信,而是与自动测试通信,而不是.
运行测试时会发生相同的行为,但是将输出重定向到文件,转义码和序列将毫无意义.
这种关系看起来像这样:
# rake test
Terminal Application
\- Bash
\- rake # Rake's $stdout.tty? => true
# (Bash is a terminal emulator)
# autotest
Terminal Application
\- Bash
\- autotest
\- rake # Rake's $stdout.tty? => false
# (Autotest is not a terminal emulator)
Run Code Online (Sandbox Code Playgroud)
有几种方法来伪造支持,因此自动测试以彩色运行,这里记录的一种方式似乎是最强大的,没有自己测试.
另一种方法是简单地使用这种技术短路"检查颜色支持"
#tty?关于流的方法不仅对上面的方法有用,而且还考虑一个运行Ruby-debug或其他一些"交互"命令的情况,当控制进程不是tty时,ruby-debug是没办法的可以提示用户,如果它连接到另一个可能不理解提示的应用程序,那么当将输出流式传输到文件,或者在另一个应用程序中运行一个进程时,智能软件总是首先检查,以查看父进程是否可能被混淆提示输入或发送非标准输出.
如果你想做一些额外的阅读,请看一下$stdin.tty?Ruby文档,它解释了被认为是ttys的进程输入和输出流之间的区别以及对事物执行方式的影响.
| 归档时间: |
|
| 查看次数: |
2354 次 |
| 最近记录: |