2011-07-20 11 views
11

Korzystając z wbudowanej frameworki Ruby Minitest, czy istnieje sposób uruchomienia kodu przed uruchomieniem całego zestawu lub nawet przed uruchomieniem całej wersji TestClass? W odpowiedzi na this question widzę, że Test :: Unit :: after_tests może być użyty do uruchomienia kodu po uruchomieniu wszystkich testów; czy istnieje podobna metoda uruchamiania kodu, zanim wszystkie testy zostaną uruchomione?Ruby Minitest: Konfiguracja na poziomie klasy lub klasy?

Chciałbym użyć tej funkcji do zainicjowania testowej bazy danych przed uruchomieniem testów i zniszczenia po ich uruchomieniu.

Dzięki!

Odpowiedz

16

Został zmodyfikowany z MiniTest docs (w ramach konfigurowalnych trybów testowych).

class Burger 
    def initialize 
    puts "YOU CREATED A BURGER" 
    end 

    def has_cheese? 
    true 
    end 

    def has_pickle? 
    false 
    end 
end 

gem 'minitest' 

require 'minitest/unit' 
MiniTest::Unit.autorun 

class MyMiniTest 
    class Unit < MiniTest::Unit 

    def before_suites 
     # code to run before the first test 
     p "Before everything" 
    end 

    def after_suites 
     # code to run after the last test 
     p "After everything" 
    end 

    def _run_suites(suites, type) 
     begin 
     before_suites 
     super(suites, type) 
     ensure 
     after_suites 
     end 
    end 

    def _run_suite(suite, type) 
     begin 
     suite.before_suite if suite.respond_to?(:before_suite) 
     super(suite, type) 
     ensure 
     suite.after_suite if suite.respond_to?(:after_suite) 
     end 
    end 

    end 
end 

MiniTest::Unit.runner = MyMiniTest::Unit.new 

class BurgerTest < MiniTest::Unit::TestCase 

    def self.before_suite 
    p "hi" 
    end 

    def self.after_suite 
    p "bye" 
    end 

    def setup 
    @burger = Burger.new 
    end 

    def test_has_cheese 
    assert_equal true, @burger.has_cheese? 
    end 

    def test_has_pickle 
    assert_equal false, @burger.has_pickle? 
    end 

end 

pamiętać, że włączyłem używać gem zamiast wersji pakietowej, które nie mają metodę MiniTest::Unit.runner. Oto wynik.

Run options: --seed 49053 

# Running tests: 

"Before everything" 
"hi" 
YOU CREATED A BURGER 
.YOU CREATED A BURGER 
."bye" 
"After everything" 


Finished tests in 0.000662s, 3021.1480 tests/s, 3021.1480 assertions/s. 

2 tests, 2 assertions, 0 failures, 0 errors, 0 skips 

Tak nazywa to #setup dwa razy, ale .before_suite i .after_suite tylko raz, co jest to, czego szukasz myślę.

+0

D'oh! Całkowicie brakowało tego w dokumentach. Dzięki! –

+3

Niestety w wersji 5 runner został usunięty ... Przypuszczam, że wyjaśnia on, dlaczego minitest ma tak wiele rozwidleń, wystarczy go załatać, zanim zadziała ... – nus

0

Alternatywnym sposobem, aby uzyskać uchwyt na czas przed i po wszystkie testy w apartamencie MiniTest został uruchomiony jest umieszczenie if bloków w setup & teardown metod, aby kontrolować te bloki, które nazywa się tylko raz.

W ten sposób można wczytać przeglądarkę i inne zależności, takie jak obiekty stron, tylko jeden raz na początku zestawu testowego, a następnie zamknąć przeglądarkę na końcu po zakończeniu wszystkich testów.

Oto przykład tego, używając MiniTest 5.5.1 i Watir:

class CoolTests < Minitest::Test 

    @@setupComplete = false # tracks whether 1-time setup has completed, so we only instantiate a browser and dependent pages/modules one time per suite run 
    @@testsRun  = 0  # tracks how many tests have run so we can close the browser when all tests complete 

    def setup             # Minitest#setup runs before every #test method 
    @@testsRun+=1            # increment tetsRun indicating that a test has run 
    if ([email protected]@setupComplete)          # we load the browser and necessary page objects here one-time if we haven't already 
     @@driver = Watir::Browser.new :chrome     # instantiate new chrome browser 
     @@driver.window.maximize        # maximize the browser window so we expect to test against Desktop UI/UX rather than Mobile UI/UX 
     @@setupComplete = true         # setupComplete is now true as we've loaded up everything we need for our tests 
    end 
    end 

    def teardown             # Minitest#teardown runs after every #test method 
    if (@@testsRun == CoolTests.runnable_methods.length) # if we've run all the tests in the suite we are finished and can then close the browser 
     @@driver.quit 
    end 
    end 

    #Tests 

    def test_one 
    p __method__ 
    @@driver.goto('www.google.com') 
    assert_equal 'Google', @@driver.title, 'browser should be at google.com' 
    end 

    def test_two 
    p __method__ 
    @@driver.goto('www.bing.com') 
    assert_equal 'Bing', @@driver.title, 'browser should be at bing.com' 
    end 
Powiązane problemy