Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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.

Trevor Brown

October 25, 2018
Tweet

More Decks by Trevor Brown

Other Decks in Programming

Transcript

  1. 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/
  2. 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
  3. 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
  4. Property Based Integration Tests • Integration tests • Use generators

    instead of hardcoded values • Generators are combined to produce numerous test cases
  5. 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
  6. 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
  7. Trevor Brown @Stratus3D Github.com/Stratus3D [email protected] • 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/