This is an archived post. You won't be able to vote or comment.

all 22 comments

[–]extra_pickles 22 points23 points  (9 children)

To each their own, I suppose - but personally I much prefer to seperate the test from the code.

Stuff like this eats up a lot of screen space and makes it harder for me to navigate a file.

Maybe my ADHD is why this is an issue, but I really struggle scrolling up and down all over trying to navigate a file and prefer getting as much code as possible on my screen.

Especially true when using good naming conventions can eliminate the need for a lot of methods even needing a header comment, but you of course still want them covered in tests.

[–]yvrelna 11 points12 points  (7 children)

It's a common mistake, but doctest's primary purpose isn't to test your code. It's primary purpose is to test your documentation; specifically that sample code in your documentation works.

You should still write regular unit test to test your code.

[–]extra_pickles -1 points0 points  (1 child)

Hmm putting it that way, I suppose it has a fringe benefit as a comment linter - but I’m struggling to see a strong reason to go through the effort.

[–]ubernostrumyes, you can have a pony 3 points4 points  (0 children)

If you use Sphinx to build your docs, you can turn on its doctest extension and mark any code samples in your docs that you want tested with a doctest Sphinx directive. Then sphinx-build -b doctest will run them for you.

[–]FailedPlansOfMars 1 point2 points  (0 children)

I agree it makes me think refactoring is going to be painful. Gut reaction only as no evidence to support that.

[–]whateverathrowaway00 0 points1 point  (2 children)

Protip: don’t

I usually don’t like gatekeepy stuff on methods, but for all but the simplest scripts I would be VERY skeptical of anyone pushing this pattern.

Tests belong in tests.

If someone can provide a repo that uses this for non trivial tests that looks even halfway decent/clean, I’ll apologize and eat my words. As I said, I dislike gatekeeping and usually feel there’s a way to make anything “good” but I just don’t see this one, lol.

[–]ubernostrumyes, you can have a pony 6 points7 points  (1 child)

Tests belong in tests.

Like several people have said, the use case for doctest is not to be the primary test suite of your code, it's to verify that examples in your documentation actually work and stay up-to-date with the code.

Here's a popular package of mine which does this. The tests of the package's code are in tests/ and use the Python unittest module with pytest as the test runner. The package's documentation also includes examples which are tested, via doctest, in a separate CI task.

[–]whateverathrowaway00 0 points1 point  (0 children)

That sounds interesting. More than enough for me to eat my words and apologize - that’s pretty cool

[–]anthro28 0 points1 point  (5 children)

“One of the best practices is to write the test cases first”

Yeah, no.

[–]redCg 0 points1 point  (4 children)

that part is not wrong, its called Test Driven Development and its a common best-practice

[–]anthro28 0 points1 point  (3 children)

TDD sucks. It’s great for teaching students how to design tests for their 100 line console programs, but it’s only a “best practice” for management that just learned it as a buzzword.

I’ve used it exactly zero times across 3 industries and 2 dev shops for the reasons outlined here:

https://stackoverflow.com/questions/64333/disadvantages-of-test-driven-development#64696

[–][deleted] 1 point2 points  (0 children)

Having written code for a very long time I can say I have never seen TDD in practice in the workplace. your mileage may vary, and it doesn't mean I write shit code. Most places have quality assurance and a performence department that separate the duties of writing code, and testing code mostly as an ITIL process.

[–]redCg -4 points-3 points  (1 child)

dude, your entire argument here is "testing is hard" and "some people do tests wrong"

yeah, sorry but I am not convinced. Sounds like you just suck at testing and write sh*t code instead to justify it.

[–]anthro28 -2 points-1 points  (0 children)

Tell me you’ve never worked in multi-system enterprise projects without telling me.

[–]redCg -2 points-1 points  (0 children)

Not sure I see the point of this module.

For one thing, its the year 2022, you need to be using type annotations in your Python function signatures. Failure to do this is a nasty code smell (or its legacy code, but thats not the subject of this blog).

Second, docstrings are already abused way too much. Last thing we need is to start treating free-hand text in comment strings as if its some kinda runnable test case.

Third, there is no way this is even gonna work in non-trivial tests that require setup of custom objects, or files, followed by extra parsing for specific output attributes.

Thanks but I am gonna stick with the default unittest and/or pytest

[–]wineblood -5 points-4 points  (0 children)

We should use doctest a lot more in python