【问题标题】:How to run only the latest/a given test using Rspec?如何使用 Rspec 仅运行最新/给定的测试?
【发布时间】:2010-03-08 14:31:46
【问题描述】:

假设我有一个包含 20 个测试的大型规范文件,因为我正在测试一个大型模型,而我没有其他方法可以做到:

describe Blah
  it "should do X" do ... end
  it "should do Y" do ... end
  ...
  it "should do Z" do ... end
end

运行单个文件比运行整个测试套件要快,但仍然很长。有没有办法运行最后一个(即文件末尾的那个,这里是“应该做Z”)?

如果这是不可能的,有没有办法指定我想在我的文件中运行哪个测试?

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    规范可以按文件或定义运行。

    Usage: spec (FILE(:LINE)?|DIRECTORY|GLOB)+ [options]
    

    实际上,它可以是这样的。运行此规范文件中的定义(确定最后一行的行)。

    spec spec/models/customer_spec.rb:25
    

    完整的语法可以通过rails根目录中的'spec'检查。

    Usage: spec (FILE(:LINE)?|DIRECTORY|GLOB)+ [options]
    
    -p, --pattern [PATTERN]          Limit files loaded to those matching this pattern. Defaults to '**/*_spec.rb'
                                     Separate multiple patterns with commas.
                                     Applies only to directories named on the command line (files
                                     named explicitly on the command line will be loaded regardless).
    -D, --diff [FORMAT]              Show diff of objects that are expected to be equal when they are not
                                     Builtin formats: unified|u|context|c
                                     You can also specify a custom differ class
                                     (in which case you should also specify --require)
    -c, --colour, --color            Show coloured (red/green) output
    -e, --example [NAME|FILE_NAME]   Execute example(s) with matching name(s). If the argument is
                                     the path to an existing file (typically generated by a previous
                                     run using --format failing_examples:file.txt), then the examples
                                     on each line of that file will be executed. If the file is empty,
                                     all examples will be run (as if --example was not specified).
    
                                     If the argument is not an existing file, then it is treated as
                                     an example name directly, causing RSpec to run just the example
                                     matching that name
    -s, --specification [NAME]       DEPRECATED - use -e instead
                                     (This will be removed when autotest works with -e)
    -l, --line LINE_NUMBER           Execute example group or example at given line.
                                     (does not work for dynamically generated examples)
    -f, --format FORMAT[:WHERE]      Specifies what format to use for output. Specify WHERE to tell
                                     the formatter where to write the output. All built-in formats
                                     expect WHERE to be a file name, and will write to $stdout if it's
                                     not specified. The --format option may be specified several times
                                     if you want several outputs
    
                                     Builtin formats:
                                     silent|l                 : No output
                                     progress|p               : Text-based progress bar
                                     profile|o                : Text-based progress bar with profiling of 10 slowest examples
                                     specdoc|s                : Code example doc strings
                                     nested|n                 : Code example doc strings with nested groups indented
                                     html|h                   : A nice HTML report
                                     failing_examples|e       : Write all failing examples - input for --example
                                     failing_example_groups|g : Write all failing example groups - input for --example
    
                                     FORMAT can also be the name of a custom formatter class
                                     (in which case you should also specify --require to load it)
    -r, --require FILE               Require FILE before running specs
                                     Useful for loading custom formatters or other extensions.
                                     If this option is used it must come before the others
    -b, --backtrace                  Output full backtrace
    -L, --loadby STRATEGY            Specify the strategy by which spec files should be loaded.
                                     STRATEGY can currently only be 'mtime' (File modification time)
                                     By default, spec files are loaded in alphabetical order if --loadby
                                     is not specified.
    -R, --reverse                    Run examples in reverse order
    -t, --timeout FLOAT              Interrupt and fail each example that doesn't complete in the
                                     specified time
    -H, --heckle CODE                If all examples pass, this will mutate the classes and methods
                                     identified by CODE little by little and run all the examples again
                                     for each mutation. The intent is that for each mutation, at least
                                     one example *should* fail, and RSpec will tell you if this is not the
                                     case. CODE should be either Some::Module, Some::Class or
                                     Some::Fabulous#method}
    -d, --dry-run                    Invokes formatters without executing the examples.
    -O, --options PATH               Read options from a file
    -G, --generate-options PATH      Generate an options file for --options
    -U, --runner RUNNER              Use a custom Runner.
    -u, --debugger                   Enable ruby-debugging.
    -X, --drb                        Run examples via DRb. (For example against script/spec_server)
        --port PORT                  Port for DRb server. (Ignored without --drb)
    -v, --version                    Show version
        --autospec
    -h, --help                       You're looking at it
    

    【讨论】:

    • 感谢您的回答,但不会跳过 before(:each) 之类的吗?
    • 没有。它基本上意味着“在该行运行场景”。显然环境会加载,功能会加载,背景等会加载然后运行场景。
    • 对不起,我花了一段时间,但我终于试了一下,效果很好
    猜你喜欢
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    相关资源
    最近更新 更多