【问题标题】:How can I get color output in autotest using Test/Unit, MiniTest?如何使用 Test/Unit、MiniTest 在自动测试中获得颜色输出?
【发布时间】:2012-02-21 20:54:58
【问题描述】:

Rails 3.2.1 应用,使用 minitest 和 autotest-rails gems。

如果我运行“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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 testunit


    【解决方案1】:

    如果我运行“rake test”,输出是彩色的。

    这是因为在自动测试下,您的测试进程正在运行的“终端”不是tty,而当您直接运行时,它是。

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

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

    通过使用终端模拟器定义的环境变量,查看它的输入和输出流(即$stdin$stdout$stderr)进程可以确定颜色支持,以及是否它连接到一个终端(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 technique987654323@短接

    流上的#tty? 方法不仅对上述情况有用,而且还考虑了在控制进程不是 tty 时运行 Ruby-debug 或其他一些“交互式”命令的情况, ruby-debug 无法提示用户,如果它连接到另一个应用程序,它可能无法理解提示,因此当将输出流式传输到文件或在另一个进程中运行一个进程时,智能软件总是首先检查,看看是否父进程可能会因提示输入或发送非标准输出而感到困惑。

    如果您想进一步阅读,请查看 Ruby 文档中的 $stdin.tty?,它解释了被视为 tty 的进程输入和输出流之间的区别以及对事物执行方式的影响。

    【讨论】:

    • 这两个链接似乎都死了:(
    • 帖子已经超过12个月了!对于提到的两种技术,此处记录的两种技术都不再适用,但理论是合理的,您必须在代码中找到,或者询问 gem 作者是否有办法强制着色,绕过检查输出是否为 tty。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多