Slide 1

Slide 1 text

Property Based Integration Testing in Ruby Trevor Brown

Slide 2

Slide 2 text

Trevor Brown Sarasota, Fl JavaScript, Erlang, Elixir, Ruby Software Developer at RentPath @Stratus3D Github.com/Stratus3D [email protected]

Slide 3

Slide 3 text

Property Based Testing

Slide 4

Slide 4 text

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/

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Property Based Integration Tests • Integration tests • Use generators instead of hardcoded values • Generators are combined to produce numerous test cases

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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/