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

The Missing Piece of Testing

Maciej
December 05, 2018

The Missing Piece of Testing

So what do you do when you write a test? Usually, you tell stories. You give examples like: for a given input I expect a given output. That’s certainly better than no tests but this approach has drawbacks.

First, you didn’t prove your code works in general – you only told some anecdotes and demonstrated it works for given examples. In other words, using those examples you’re trying to prove there are no bugs. Shouldn’t your tests help find bugs instead?

Second, you only covered some general cases and edge cases you know. What about edge cases you don’t know?

As you can see, even though you’re doing your best, you may still be missing a fundamental piece of testing. A piece that empowers you to describe behavior instead of output, to find bugs more effectively, and to test more cases with less code.

In my talk, I’ll show you how you can leverage the power of property-based testing to complement your testing tool belt and have even more confidence with the software you create.

Maciej

December 05, 2018
Tweet

More Decks by Maciej

Other Decks in Programming

Transcript

  1. describe('sort function', () => { it('should sort integers', () =>

    { expect(sort([3, 2])).toEqual([2, 3]) }) })
  2. describe('sort function', () => { it('should sort integers', () =>

    { expect(sort([3, 2])).toEqual([2, 3]) }) }) }) })
  3. describe('sort function', () => { it('should sort integers', () =>

    { generate(array(integer()), (unsortedArr) => {}) }) })
  4. describe('sort function', () => { it('should sort integers', () =>

    { generate(array(integer()), (unsortedArr) => { sort(unsortedArr) }) }) })
  5. }) }) }) describe('sort function', () => { it('should sort

    integers', () => { generate(array(integer()), (unsortedArr) => { expect(sort(unsortedArr)).toEqual( /* what */)
  6. result.forEach((element, i) => { const nextElement = sorted[i + 1]

    if (nextElement) { expect(nextElement).toBeGreaterThan(element) } }) const sorted = sort(unsortedArr) // the next element should be greater than the previous one
  7. describe('sort function', () => { it('should sort integers', () =>

    { generate(array(integer()), (unsortedArr) => { // assertions }) }) })
  8. describe('sort function', () => { it('should sort integers', () =>

    { runMultipleTimes( generate(array(integer()), (unsortedArr) => { // assertions }) ) }) })
  9. import fc from 'fast-check' describe('sort function', () => { it('should

    sort integers', () => { runMultipleTimes( generate(array(integer()), (unsortedArr) => { // assertions }) ) }) })
  10. import fc from 'fast-check' describe('sort function', () => { it('should

    sort integers', () => { fc.assert( generate(array(integer()), (unsortedArr) => { // assertions }) ) }) })
  11. import fc from 'fast-check' describe('sort function', () => { it('should

    sort integers', () => { fc.assert( fc.property(array(integer()), (unsortedArr) => { // assertions }) ) }) })
  12. import fc from 'fast-check' describe('sort function', () => { it('should

    sort integers', () => { fc.assert( fc.property(fc.array(fc.integer()), (unsortedArr) => { // assertions }) ) }) })
  13. import fc from 'fast-check' describe('sort function', () => { it('should

    sort integers', () => { fc.assert( fc.property(fc.array(fc.integer()), (unsortedArr) => { // assertions }) ) expect(sort([3, 2])).toEqual([2, 3]) }) })
  14. import fc from 'fast-check' describe('sort function', () => { it('should

    sort integers', () => { fc.assert( fc.property(fc.array(fc.integer()), (unsortedArr) => { // assertions }) ) expect(sort([3, 2])).toEqual([2, 3]) }) })
  15. import fc from 'fast-check' describe('sort function', () => { it('should

    sort integers', () => { fc.assert( fc.property(fc.array(fc.integer()), (unsortedArr) => { // assertions }) ) expect(sort([3, 2])).toEqual([2, 3]) expect(sort([10, 2])).toEqual([2, 10]) }) })