开发者

Best way to associate data files with particular tests in RSpec / Ruby

开发者 https://www.devze.com 2023-01-02 00:39 出处:网络
For my RSpec tests I would to automatically associate data files with each test. To clarify, if my tests each require an xml file as input data and then some xpath statements to validate the responses

For my RSpec tests I would to automatically associate data files with each test. To clarify, if my tests each require an xml file as input data and then some xpath statements to validate the responses they get back I would like to externalize the xml and xpath as files and have the testing framework easily associate them with the particular test being run by using the unique ID of the test as the file(s) name. I tried to get this behavior but the solution isn't very clean. I wrote a helper method that takes the value of "description" and combines it with FILE to create a unique identifier which is set into a global variable that other utilities can access. The unique identifier is used to associate the data files I need. I have to call this helper method as the first line of every test, which is ugly.

If I have an RSpec example that looks like this:

describe "Basic functions of this server I'm testing" do
  it "should give me back a response" do
    # Sets a global var to: "my_tests_spec.rb_should_give_me_back_a_response"
    TestHelper::who_am_i __FILE__, description
    ...
  end
end

Is there some better/cleaner/slicker way I 开发者_Go百科can get an unique ID for each test that I could use to associate data files with? Perhaps something build into RSpec I'm unaware of?

Thank you, -Bill


I just learned about the nifty global before and after hooks. I can hide the unique ID creation code there. It makes things much cleaner. I'll probably go with this solution unless there's an even slicker way to acquire a unique ID for each test. Thanks

0

精彩评论

暂无评论...
验证码 换一张
取 消