Pro Yearly is on sale from $80 to $50! »

Property Based Integration Testing in Ruby

Property Based Integration Testing in Ruby

Integration testing is easy when you fully control the system you are testing. Things become difficult when you can't control the system, or the data in the system, and are unable to know exactly how it will behave. Property based testing allows you to define certain properties you expect your system to have, and then the property testing framework will generate and run tests for your system. In this talk I'll discuss how I used property based testing to test REST API in Ruby.

9fa60df551b9e4282c0519bf173c2e04?s=128

Trevor Brown

October 25, 2018
Tweet

Transcript

  1. Property Based Integration Testing in Ruby Trevor Brown

  2. Trevor Brown Sarasota, Fl JavaScript, Erlang, Elixir, Ruby Software Developer

    at RentPath @Stratus3D Github.com/Stratus3D admin@stratus3d.com
  3. Property Based Testing

  4. Property Based Testing “Property based testing is the construction of

    tests such that, when these tests are fuzzed, failures in the test reveal problems with the system under test…” – https://hypothesis.works/articles/what-is-property- based-testing/
  5. Integration Testing "Integration testing (sometimes called integration and testing, abbreviated

    I&T) is the phase in software testing in which individual software modules are combined and tested as a group." – https://en.wikipedia.org/wiki/Integration_testing
  6. Integration Testing Limitations • Integration testing becomes difficult when –

    You do not control part or all of the system – You cannot control the data in the system – The system is in production
  7. Property Based Integration Tests • Integration tests • Use generators

    instead of hardcoded values • Generators are combined to produce numerous test cases
  8. describe '/v3/cities' do let(:url) { '/v3/cities' } it "returns a

    list of cities" do params = { state: 'california', limit: 10 } get url, params # It was a success expect(last_response.status).to eq(200) # Has the expected structure expect(json_body).to be_a(Array) # Has the expected number of results expect(json_body.count).to be >= 0 expect(json_body.count).to be <= 10 # Results have the expected fields json_body.each do |record| expect(record.keys.sort).to eq(%w(name state url)) end # Results are sorted properly if json_body.first and json_body.second expect(json_body.first['name']).to be < json_body.second['name'] end end end
  9. describe '/v3/cities' do it "returns a list of cities" do

    property_of do state = choose(*states) limit = range(0, 25) [state, limit] end.check do |(state, limit)| params = { state: space_to_dash(state), limit: limit } get url, params # It was a success expect(last_response.status).to eq(200) # Has the expected structure expect(json_body).to be_a(Array) # Has the expected number of results expect(json_body.count).to be >= 0 expect(json_body.count).to be <= limit # Results have the expected fields json_body.each do |record| expect(record.keys.sort).to eq(%w(name state url)) end # Results are sorted properly if json_body.first and json_body.second expect(json_body.first['name']).to be < json_body.second['name'] end end end end
  10. Trevor Brown @Stratus3D Github.com/Stratus3D admin@stratus3d.com • https://speakerdeck.com/stratus3d/property-based-integration-testing-in-ruby • https://www.youtube.com/watch?v=OVLTHGaTi7k •

    http://blog.jessitron.com/2014/09/tdd-with-generative-testing-example-in.html • https://www.sitepoint.com/the-how-and-why-of-property-based-testing-in-ruby/