![](/img/trans.png)
[英]How can I get Minitest to resolve helper in a controller test?
[英]How can I get color output in autotest using Test/Unit, MiniTest?
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
如果我運行“rake test”,則輸出為彩色。
這是因為在自動測試中,運行測試進程的“終端”不是tty
,而當你直接運行時,它就是。
首先它是如何工作的,顏色代碼在轉義序列中定義,如果你把它們寫下來就會像\\E[48mPRINT ME IN RED\\E[0m
( 細節 ))。
您的終端了解這些轉義序列(通常),用顏色替換它們,改善輸出的外觀。
通過使用終端仿真器定義的環境變量,並查看它的輸入,輸出流(即$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)
有幾種方法來偽造支持,因此自動測試以彩色運行, 這里記錄的一種方式似乎是最強大的,沒有自己測試。
另一種方法是簡單地使用這種技術短路“檢查顏色支持”
方法#tty?
在流上不僅對上面有用,還考慮一個運行Ruby-debug或其他一些“交互”命令的情況,當控制進程不是tty時,ruby-debug無法提示用戶,如果它連接到另一個可能無法理解提示的應用程序,那么當將輸出流式傳輸到文件,或者在另一個應用程序中運行一個進程時,智能軟件總是首先檢查,以查看父進程是否可能因提示輸入而混淆輸入或發送非標准輸出。
如果你想做一些額外的閱讀,請看看$stdin.tty?
從Ruby文檔中,它解釋了被認為是ttys的進程輸入和輸出流之間的區別以及對事物執行方式的影響。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.