Giter Site home page Giter Site logo

ikding / escape-from-automanual-testing Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zac-hd/escape-from-automanual-testing

0.0 2.0 0.0 3.78 MB

A three-hour tutorial on property-based testing with https://hypothesis.works

License: GNU Affero General Public License v3.0

Python 100.00%

escape-from-automanual-testing's Introduction

Escape from automanual testing with Hypothesis!

A three-hour workshop on property-based testing with Hypothesis, first delivered at PyCon 2019.

It is designed for intermediate Python users, with extension activities up to expert level. Some minimal experience of unit testing and pytest are assumed - if you have ever used @pytest.mark.parametrize, you are overqualified; but if you have never written and run unit tests you may have trouble.

How this workshop works

It's a crash-course in the important concepts, alternating between short talks and hands-on excercises. You'll get to know the architecture of the library, but not every detail of the API - you can look up that documentation at any time.

Setting up

Clone this repository, and pip install pytest hypothesis. That's it!

Hypothesis is also available on conda-forge, if you prefer to use conda. For the SciPy edition of this workshop, there are optional excercises that require Numpy and Pandas.

To test that everything is installed correctly, run pytest pbt-101.py. You should see nine passing tests and no errors.

Any recent version of pytest, and any hypothesis>=4.0. Use whatever package manager and environment you prefer - if in doubt, just pip install as above. Hypothesis is compatible with every currently supported version of Python, i.e. 2.7, 3.5, 3.6, and 3.7 - and a similarly wide range of versions for it's optional dependencies.

Property-based testing 101

Each block starts with a short talk (slides separated by a blank black slide), followed by a hands-on excercise where you can apply what you've just learned. In this first block we'll see a taxonomy of testing techniques, define property-based testing, and get an initial overview of Hypothesis.

After the talk:

  1. Follow the "Setting up" instructions above
  2. Run pytest pbt-101.py. You should see several passing tests.
  3. Open the file in your preferred editor and read the detailed instructions within! (in short: fix the test, check the test fails, fix the test, check that it passes)

Hypothesis strategies and property-based "tactics"

This block aims to get you comfortable and productive with Hypothesis, which means covering two things: how to generate all kinds of data, and how to use it in your tests.

strategies are objects which tell @given what to pass to your test function. Hypothesis ships with dozens for standard library types and optional dependencies such as pytz, Django, Numpy, and Pandas - to say nothing of third-party extensions! We'll see what's available to explicitly describe your data or infer it from a schema (e.g. strings from a regular expression), and how you can combine, compose and adjust strategies to produce something quite different.

"Tactics" are design patterns for property based tests. They range from common properties to test, through to embedding assertions in your code (not just tests) for free integration tests, and more.

After the talk, pytest strategies-and-tactics.py, and continue as above.

Testing the Untestable or Scientific Hypothesis

Nothing is ever really untestable - but sometimes you need better tools to make testing worth the trouble. We'll explore two approaches:

  • Stateful testing, where you define a finite automaton and Hypothesis uses it to generate whole test programs (by choosing actions as well as values).
  • Metamorphic testing, where you don't know exactly what the code should do - but do know something about how changes in the input relate to changes in the output.

The excercises in test-the-untestable.py are deliberately challenging. Choose whichever one is the most interesting to you, and don't worry if it takes you the whole block in class - you can always come back to the others later.

Alternatively, if you use the Numpy / Pandas stack, scientific-hypothesis.py is full of excercises that demonstrate Hypothesis' support for generating arrays, dataframes, and all the related things you might need to test data-centric scripts

  • or libraries!

The bigger picture

We'll discuss Hypothesis' performance characteristics and configuration options, get a sense of the community around it and the project roadmap, and look at how Hypothesis fits into the wider testing and correctness landscape in Python.

For this final block, you have a few options:

  • Continue working on any unfinished exercises from previous blocks
  • Start testing your own code with Hypothesis
  • Join a group discussion - what to you plan to test? What 'killer feature' is missing?

Useful links

As well as the links above, you may be interested in:

escape-from-automanual-testing's People

Contributors

ikding avatar kathyreid avatar rsokl avatar zac-hd avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.