Live data from Hacker News

Show HN: Python Tests That Write Themselves

timothycrosley.github.io

21–30 of 39 posts

Re: Show HN: Python Tests That Write Themselves

#21
This actually gave me another idea. What do you all think, I’d be up for trying to build it.

It would watch your program during execution and record each function calls input and output. And then create tests for each function using those inputs and outputs.

You could always go over the created tests manually and fix them or review them but if nothing else it could be a good start.

Re: Show HN: Python Tests That Write Themselves

#22

This actually gave me another idea. What do you all think, I’d be up for trying to build it. It would watch your program during execution and record each function calls input and output. And then create tests for each function using those inputs and outputs. You could always go over the created tests manually and fix them or review them but if nothing else it could be a good start.

This is basically what they call snapshot testing

Re: Show HN: Python Tests That Write Themselves

#23

This actually gave me another idea. What do you all think, I’d be up for trying to build it. It would watch your program during execution and record each function calls input and output. And then create tests for each function using those inputs and outputs. You could always go over the created tests manually and fix them or review them but if nothing else it could be a good start.

I really like this idea. So, broken out, would the development process look like: 1) Write code 2) Open interpreter (ipython) 3) Import code and call it manually 4) This magic library will have observed and created a testcase module from your execution

If the library can use the observed and recorded testcase to extrapolate far beyond your manual call, this could be a really interesting way of testing python code.

Re: Show HN: Python Tests That Write Themselves

#24

This actually gave me another idea. What do you all think, I’d be up for trying to build it. It would watch your program during execution and record each function calls input and output. And then create tests for each function using those inputs and outputs. You could always go over the created tests manually and fix them or review them but if nothing else it could be a good start.

I remember finding something like this a few years ago for testing rest apis. It was very cool but for the life of me I can't recall the name :(

Re: Show HN: Python Tests That Write Themselves

#25

This actually gave me another idea. What do you all think, I’d be up for trying to build it. It would watch your program during execution and record each function calls input and output. And then create tests for each function using those inputs and outputs. You could always go over the created tests manually and fix them or review them but if nothing else it could be a good start.

Instagram created 'MonkeyType' to do something similar for type annotations.

A lower barrier way of trying this out could be to run MonkeyType in combination with OP's hypothesis-auto (which uses type annotations to generate the tests).

Re: Show HN: Python Tests That Write Themselves

#26

This is neatly packaged, but it's not immediately clear what advantages it has over Hypothesis' native offerings for accomplishing this? ( https://hypothesis.readthedocs.io/en/latest/details.html#inf... and https://hypothesis.readthedocs.io/en/latest/data.html#hypoth... )

Hypothesis' `infer` is inferring arguments to the test fn, that you'd then use to drive the fn under test.

OP's extension is inferring arguments to the fn under test, and then generating a test like 'assert isinstancee(fn_under_test(a, *kw), expected_ret_type)` to go with them.

You can do more types of test with `infer` (i.e. not just 'does return' and 'returns correct type' but also 'returns correct value'), but this is an easy way to cover a lot of basic return checking ground.

Re: Show HN: Python Tests That Write Themselves

#27

This actually gave me another idea. What do you all think, I’d be up for trying to build it. It would watch your program during execution and record each function calls input and output. And then create tests for each function using those inputs and outputs. You could always go over the created tests manually and fix them or review them but if nothing else it could be a good start.

I came across something similar for Clojure recently. https://github.com/ahungry/determinism/

Re: Show HN: Python Tests That Write Themselves

#28

This actually gave me another idea. What do you all think, I’d be up for trying to build it. It would watch your program during execution and record each function calls input and output. And then create tests for each function using those inputs and outputs. You could always go over the created tests manually and fix them or review them but if nothing else it could be a good start.

I'd use it!

Re: Show HN: Python Tests That Write Themselves

#29
On one job, I had to disable code coverage for a whole suite of tests that were simply making calls and completely ignoring the results.

I know, coverage is Yet Another Metric, but if you don't game it, it can help you track down branches you haven't written tests for.

So my hesitation is I can see people running this, gettting 100% code coverage and thinking, "hooray, it's fully tested!"

Re: Show HN: Python Tests That Write Themselves

#30
post #25

This actually gave me another idea. What do you all think, I’d be up for trying to build it. It would watch your program during execution and record each function calls input and output. And then create tests for each function using those inputs and outputs. You could always go over the created tests manually and fix them or review them but if nothing else it could be a good start.

Instagram created 'MonkeyType' to do something similar for type annotations. A lower barrier way of trying this out could be to run MonkeyType in combination with OP's hypothesis-auto (which uses type annotations to generate the tests).

pytype (Google) [1], PyAnnotate (Dropbox) [2], and MonkeyType (Instagram) [3] all do dynamic / runtime PEP-484 type annotation type inference [4]

[1] https://github.com/google/pytype

[2] https://github.com/dropbox/pyannotate

[3] https://github.com/Instagram/MonkeyType

[4] https://news.ycombinator.com/item?id=19454411

Post reply on HN