TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Python Tests That Write Themselves

131 pointsby timothycrosleyover 5 years ago

10 comments

mrfusionover 5 years ago
This actually gave me another idea. What do you all think, I’d be up for trying to build it.<p>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.<p>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.
评论 #21010736 未加载
评论 #21011145 未加载
评论 #21013401 未加载
评论 #21010305 未加载
评论 #21010342 未加载
评论 #21010591 未加载
评论 #21011190 未加载
ebg13over 5 years ago
Does this do anything other than assert that functions return the right basic type given extremely basic inputs?<p>Is naive type testing a thing that people actually bother doing vs testing that the functions actually do what they&#x27;re supposed to?<p>If a function takes two ints and returns an int that is the integer division of the two, is the evaluation of exceptions and returned types not already implicit in the evaluation of the actual results?<p>If you know that divide(a, b) should return c for sufficient candidates a, b, and c, then you _know_ that divide returns the right type without explicitly checking. And knowing that the divide function happens to return ints when given ints doesn&#x27;t actually tell you that it&#x27;s doing anything even close to the right behavior. So this both doesn&#x27;t reduce the number of tests you need to write and is also obsoleted by actually writing the tests that you need.
评论 #21008310 未加载
评论 #21007826 未加载
rcfoxover 5 years ago
I like the idea, but I think I would be more comfortable with it generating test files that I could keep beside my other tests.<p>Also, `auto_pytest_magic` doesn&#x27;t seem to exist.<p><pre><code> ImportError: cannot import name &#x27;auto_pytest_magic&#x27; from &#x27;hypothesis_auto&#x27; (&#x2F;home&#x2F;...&#x2F;.local&#x2F;lib&#x2F;python3.7&#x2F;site-packages&#x2F;hypothesis_auto&#x2F;__init__.py)</code></pre>
评论 #21008694 未加载
hchasestevensover 5 years ago
This is neatly packaged, but it&#x27;s not immediately clear what advantages it has over Hypothesis&#x27; native offerings for accomplishing this? ( <a href="https:&#x2F;&#x2F;hypothesis.readthedocs.io&#x2F;en&#x2F;latest&#x2F;details.html#inferred-strategies" rel="nofollow">https:&#x2F;&#x2F;hypothesis.readthedocs.io&#x2F;en&#x2F;latest&#x2F;details.html#inf...</a> and <a href="https:&#x2F;&#x2F;hypothesis.readthedocs.io&#x2F;en&#x2F;latest&#x2F;data.html#hypothesis.strategies.from_type" rel="nofollow">https:&#x2F;&#x2F;hypothesis.readthedocs.io&#x2F;en&#x2F;latest&#x2F;data.html#hypoth...</a> )
评论 #21007885 未加载
评论 #21010794 未加载
ben509over 5 years ago
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.<p>I know, coverage is Yet Another Metric, but if you don&#x27;t game it, it can help you track down branches you haven&#x27;t written tests for.<p>So my hesitation is I can see people running this, gettting 100% code coverage and thinking, &quot;hooray, it&#x27;s fully tested!&quot;
timothycrosleyover 5 years ago
Thoughts behind project creation live here: <a href="https:&#x2F;&#x2F;timothycrosley.com&#x2F;project-5-hypothesis-auto" rel="nofollow">https:&#x2F;&#x2F;timothycrosley.com&#x2F;project-5-hypothesis-auto</a>
评论 #21008215 未加载
iandanforthover 5 years ago
I like this idea. One piece of feedback, a parameter with a leading underscore feels very odd. In python I interpret leading underscores to indicate the programmer thinks of this as an internal &#x2F; pseudo-private property. Exposing it through the api makes it &quot;public&quot; which means (to me) that it shouldn&#x27;t have a leading underscore.<p>This is especially true if the usecase is common enough to put in the top level examples
评论 #21007859 未加载
somada141over 5 years ago
While I had really liked the idea of hypothesis in Python I found that the edge-cases it was uncovering were the ones that were obviously gonna break but at the same time cases I didn&#x27;t care to guard against, e.g., using 3-mile long integers, or cases that wouldn&#x27;t work with the underlying libraries eg NumPy. Thus, I found myself spending more time adding constraints on the generated inputs than fledging out my test-suite. So my adventures with hypothesis were short-lived.<p>I don&#x27;t mean to detract from this library, I think its a great combination of strong-typing and property-based testing but has anyone had any experience employing property-based testing on complex functions outside of the whole add&#x2F;subtract&#x2F;multiple stuff? What kinda thing have you used it on?
StavrosKover 5 years ago
This is great, thank you! Another great example of the benefits strong typing brings.
kburmanover 5 years ago
Nice concept but does it work with real-world application. I failed to understand how will it work with methods like `authenticate_user(user)` or `load_permissions_from_db(user, db)`.
评论 #21006564 未加载