How to determine the general setup and break logic for all tests in ruby ​​Test :: Unit :: TestCase?

Suppose there are potentially costly operations in setup or teardown that are the same for all tests and whose results will not be confused during test runs. It seems to me wrong that they run before / after each test.

So, is there a preferred way to run the install / break code only before the first test is done and only after the last test is done?

Change In the specific case, I should check some extensions for Net :: FTP and thus establish an FTP connection and configure some remote objects for testing:

 class TestFTPExtensions < Test::Unit::TestCase def setup # Setup connection @ftp = Net::FTP.new 'localhost', 'anonymous' @ftp.passive = true # Create remote test directory @ftp.mkdir 'dir' # Create remote test file path = File.join Dir.tmpdir, 'file' File.open path, 'w' do |f| @ftp.put f end File.delete path end def teardown @ftp.rmdir 'dir' @ftp.delete 'file' @ftp.close end # imagine some tests here that don't change/remove any remote objects end 
+4
source share
1 answer

Thanks to Andrew, I found the answer to this fooobar.com/questions/196353 / ....

However, while searching for an answer, I also noticed that in the 1.9.x branch, the standard testing environment was switched to MiniTest. So actually I am using this for testing right now. This answer explains how to achieve this with MiniTest.

+4
source

Source: https://habr.com/ru/post/1394474/


All Articles