Class: Roby::App::Rake::TestTask

Inherits:
BaseTestTask show all
Defined in:
lib/roby/app/rake.rb

Overview

Rake task to run the Roby tests

To use, add the following to your Rakefile:

require 'roby/app/rake'
Roby::App::Rake::TestTask.new

It create a test task per robot configuration, named “test:$robot_name”. It also creates a test:all-robots task that runs each robot’s configuration in sequence. You can inspect these tasks with

rake --tasks

and call them with e.g.

rake test:default

The test:all-robots task will fail only at the end of all tests, reporting which configuration actually failed. To stop at the first failure, pass a ‘0’ as argument, e.g.

rake 'test:all-robots[0]'

Finally, the ‘test:all’ target runs syskit test –all (i.e. runs all tests in the default robot configuration)

The ‘test’ target points by default to test:all-robots. See below to change this.

The following examples show how to fine-tune the creates tests:

Examples:

restrict the tests to run only under the

'only_this_configuration' configuration

   Roby::App::Rake::TestTask.new do |t|
       t.robot_names = ['only_this_configuration']
   end

create tasks under the roby_tests namespace instead of ‘test’


Roby::App::Rake::TestCase.new('roby_tests')

make ‘test’ point to ‘test:all’


Roby::App::Rake::TestCase.new do |t|
    t.all_by_default = true
end

Defined Under Namespace

Classes: Failed

Instance Attribute Summary collapse

Attributes inherited from BaseTestTask

#app, #base_dir, #config, #excludes, #force_discovery, #report_dir, #self_only, #task_name, #test_files, #ui

Instance Method Summary collapse

Methods inherited from BaseTestTask

#coverage?, #force_discovery?, #keep_logs?, #read_captured_output_from_pipe, #run_roby, #run_roby_test, #self_only?, #spawn_process, #spawn_process_capturing_output, #ui?, #use_junit?, #wait_process_with_captured_output, #write_captured_output

Constructor Details

#initialize(task_name = "test", all_by_default: false) {|_self| ... } ⇒ TestTask

Returns a new instance of TestTask.

Yields:

  • (_self)

Yield Parameters:



428
429
430
431
432
433
434
435
436
# File 'lib/roby/app/rake.rb', line 428

def initialize(task_name = "test", all_by_default: false)
    super(task_name)

    @all_by_default = all_by_default

    @robot_names = discover_robot_names
    yield self if block_given?
    define
end

Instance Attribute Details

#robot_namesArray<String,(String,String)>

The list of robot configurations on which we should run the tests

Use ‘default’ for the default configuration. It defaults to all the robots defined in config/robots/

Returns:

  • (Array<String,(String,String)>)


422
423
424
# File 'lib/roby/app/rake.rb', line 422

def robot_names
  @robot_names
end

Instance Method Details

#defineObject



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/roby/app/rake.rb', line 448

def define
    test_args = %i[keep_going synchronize_output omit_tests_success]

    each_robot do |robot_name, robot_type|
        task_name = task_name_for_robot(robot_name, robot_type)

        desc "run the tests for configuration #{robot_name}:#{robot_type}"
        task task_name, test_args do |t, args|
            synchronize_output =
                args.fetch(:synchronize_output, "0") == "1"
            omit_tests_success =
                args.fetch(:omit_tests_success, "0") == "1"
            result = run_roby_test(
                "-r", "#{robot_name},#{robot_type}",
                coverage_name: task_name,
                report_name: "#{robot_name}:#{robot_type}",
                synchronize_output: synchronize_output,
                omit_success: omit_tests_success
            )
            unless result
                raise Failed.new("failed to run tests for "\
                                 "#{robot_name}:#{robot_type}"),
                      "tests failed"
            end
        end
    end

    desc "run tests for all known robots"
    task "#{task_name}:all-robots", test_args do |t, args|
        failures = []
        keep_going = args.fetch(:keep_going, "1") == "1"
        synchronize_output = args.fetch(:synchronize_output, "0") == "1"
        omit_tests_success = args.fetch(:omit_tests_success, "0") == "1"
        each_robot do |robot_name, robot_type|
            coverage_name = "#{task_name}:all-robots:"\
                            "#{robot_name}-#{robot_type}"
            success = run_roby_test(
                "-r", "#{robot_name},#{robot_type}",
                coverage_name: coverage_name,
                report_name: "#{robot_name}:#{robot_type}",
                synchronize_output: synchronize_output,
                omit_success: omit_tests_success
            )

            unless success
                failures << [robot_name, robot_type]
                handle_test_failures(failures) unless keep_going
            end
        end

        handle_test_failures(failures)
    end

    desc "run all tests"
    task "#{task_name}:all", test_args do |t, args|
        synchronize_output = args.fetch(:synchronize_output, "0") == "1"
        omit_tests_success = args.fetch(:omit_tests_success, "0") == "1"
        unless run_roby_test(coverage_name: "all",
                             synchronize_output: synchronize_output,
                             omit_success: omit_tests_success,
                             report_name: task_name)
            raise Failed.new("failed to run tests"),
                  "failed to run tests"
        end
    end

    if all_by_default?
        desc "run all tests"
        task task_name, test_args => "#{task_name}:all"
    else
        desc "run all robot tests"
        task task_name, test_args => "#{task_name}:all-robots"
    end
end

#discover_robot_namesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Discover which robots are available on the current app



541
542
543
544
545
# File 'lib/roby/app/rake.rb', line 541

def discover_robot_names
    app.guess_app_dir unless app.app_dir
    app.setup_robot_names_from_config_dir
    app.robots.each.to_a
end

#each_robot(&block) ⇒ Object

Enumerate the robots on which tests should be run



534
535
536
# File 'lib/roby/app/rake.rb', line 534

def each_robot(&block)
    robot_names.each(&block)
end

#handle_test_failures(failures) ⇒ Object

Raises:

  • (Failed.new("failed to run the following test(s): "\ "#{msg}"))


523
524
525
526
527
528
529
530
531
# File 'lib/roby/app/rake.rb', line 523

def handle_test_failures(failures)
    return if failures.empty?

    msg = failures
          .map { |name, type| "#{name}:#{type}" }
          .join(", ")
    raise Failed.new("failed to run the following test(s): "\
                     "#{msg}"), "failed to run tests"
end

#task_name_for_robot(robot_name, robot_type) ⇒ Object



440
441
442
443
444
445
446
# File 'lib/roby/app/rake.rb', line 440

def task_name_for_robot(robot_name, robot_type)
    if robot_name == robot_type
        "#{task_name}:#{robot_name}"
    else
        "#{task_name}:#{robot_name}-#{robot_type}"
    end
end