News for hackers. Stuff that matters.
Re:Hypothesis: Property-Based Testing for Python by wongarsu on Wednesday November 05, 2025 @04:36AM (#45821137)
Imho it's just not a great example. A better example would be testing the invariants of the function. "For any list of numbers, my_sort does not throw an exception" is trivial to test. "For any list of numbers, in the list returned by my_sort the item at index n is smaller than the item at index n+1" would be another test. Those two probably capture the full specification of my_sort, and you don't need a sort function to test either. In a real-world example you would more likely be testing just some subset of the specification, those things that are easy to assert
Re:Hypothesis: Property-Based Testing for Python by lgas on Wednesday November 05, 2025 @05:39AM (#45821481)
Always returning the empty list meets your spec.
Re:Hypothesis: Property-Based Testing for Python by wongarsu on Wednesday November 05, 2025 @05:50AM (#45821530)
Good point. I suppose we should add "number of input elements equals number of output elements" and "every input element is present in the output". Translated in a straightforward test that still allows my_sort([1,1,2]) to return [1,2,2], but we have to draw the line somewhere
Re:Hypothesis: Property-Based Testing for Python by masklinn on Wednesday November 05, 2025 @11:44AM (#45824990)
> we have to draw the line somewhere

Do we? You can pop-count the two lists and checks that those are equal.

Re:Hypothesis: Property-Based Testing for Python by travisjungroth on Wednesday November 05, 2025 @01:42PM (#45826305)
Just use Counter and if the objects aren’t hashable, use the count of IDs. Grab this before calling the function, in case the function is destructive. Check it against the output.

Add in checking each item is less than or equal to its successor and you have the fundamental sort properties. You might have more, like stability.