2012-02-21 52 views
6

的Rails 3.2.1应用程序,使用MINITEST和自动测试护栏宝石。如何使用测试/单元MiniTest在自动测试中获得彩色输出?

如果我运行“耙测试”的输出是彩色的。但是如果我运行自动测试,输出不是彩色的。

如何使用自动测试时,我得到的彩色输出?

这里是我的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 

回答

7

如果我运行 “耙测试” 的输出是彩色的。

这是因为在自动测试“终端”您的测试processess在运行不是tty,当你直接运行,它是。

首先它是如何工作的,颜色代码是在转义序列中定义的,如果你把它们写下来看起来像\E[48mPRINT ME IN RED\E[0mdetails)。

您的终端理解这些转义序列(通常),用颜色替换它们,改善输出的外观。

通过使用由终端模拟器中定义的环境变量,看它是它的输入,和输出流(即$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) 

有一些方法来伪造的支持,使自动测试在颜色上运行,documented here似乎是最强大的,而不必测试它自己的一种方式。

另一种方式,是简单地短路它的“检查颜色支持”的流using this technique

方法#tty?不是上述只是有用的,但也考虑到一个情况运行Ruby-debug或其他一些“交互式”命令,当控制进程不是tty时,ruby-debug无法提示用户,如果它连接到另一个可能不理解提示的应用程序,那么当将输出流式传输到一个文件,或者在另一个进程中运行一个进程,智能软件总是首先检查,通过提示输入或发送非标准输出来查看父进程是否可能被混淆。

如果您想做一些额外的阅读,请参阅Ruby文档中的$stdin.tty?,它解释了输入输出流被认为是ttys的过程与对事物执行方式的影响之间的区别。

+2

这两个环节似乎是死:( – alnorth29 2013-09-30 06:39:04

+2

的职位超过12个月大的!对于这两种技术中提到,无论是在这里介绍的技术不工作了,但理论上是合理的,你必须在代码中查找,或者询问宝石作者是否有强制着色的方法,绕过检查输出是否为tty。 – 2013-09-30 07:06:23

相关问题