Pytest Tips and Tricks
pythontest.com
pythontest.com
Which meaning did you intend?
The trick might be not using an IDE at all, I consider python and most of its frameworks explicit enough to write with editors like vim. I never found a need to use an IDE for python even when the projects gets larger, because even then the unix tools seemed to work much better at whatever that IDE is doing. This compares sharply against languages in the JVM family.
If you're instantiating the same fixture name across a bunch of different tests, using similar fixtures in different locations and not use import or a central or hierarchical location to define your fixtures after you have accumulated a number of similar items, I find it hard to blame the module for not being explicit.
I have to use `pytest --fixtures | less` and search for the fixture's name (or `pytest --fixtures | grep -C 50`)
> If you're instantiating the same fixture name across a bunch of different tests
It means I cannot name my fixture `database`, I have to call it `myplugin_database` instead to make sure it won't conflict with other plugins. And tests using this fixture have the long `myplugin_database` as argument.
Python already had namespaces as first-class citizens, it's a shame we have to go back to name prefixes.
I wanted to integrate pytest-benchmark[1]. Their docs mention fixtures a couple of times, but the examples only show that test functions receive a magic untyped parameter called "benchmark".
How would I make my usage more explicit? Can I somehow explicitly import the fixture, or at least type it?
[1] https://pytest-benchmark.readthedocs.io/en/latest/index.html
On the subject of implicit vs explicit, `myplugin_database` is unquestionably more explicit.
Yes, likewise after you understand how it works. It's great. That's not what I am arguing about.
Pytest could have been designed more explicitly while retaining it's advantages. I am a huge fan of how I can just use "==" operators to test equality. The UX of pytest is great after you know how it works.
> that it makes learning the library difficult for new comers
Not only for newcomers, I've used in the past and if I need to pick it up again I believe I'm going to spend some time refreshing a ton of things and probably get got caught by some caveat.
> fixtures are passed as arguments in function
Exactly questioned that myself so long ago. Implementing fixture functionality from scratch in Python (even supporting different formats) is only a few lines of code.
> There are dozens of things that go completely against the philosophy of Python: Explicit is better than implicit.
I have found that this is not just true of pytest, but also of the python language! :P
Curious as to how? It is pretty explicit and simple when compared against the pack.
I always use a notebook for notes and experiments when building python packages. By using `jupytext`[0] I can store the notebook as a commented python-file, which I can run from a parametrized test that runs all notebooks in a given folder [1]. Adding a few `asserts`, this gives you reasonable test coverage right from the start.
[0]: https://jupytext.readthedocs.io/en/latest/ [1]: https://stackoverflow.com/a/56813896/212538
- https://talkpython.fm/episodes/show/407/pytest-tips-and-tric...
It hangs forever on the `collecting ...` phase.
Alternatively, your IDE or editor probably already has an extension that allows you to right click - run single test. VS Code at least has some plugin that collects the tests in the background.
=============================== warnings summary ===============================
tests/parser/test_expressions.py::test_complex[+inf+1.23e7j-(inf+12300000j)]
You can run just that particular test (with that particular parametrized input) with pytest tests/parser/test_expressions.py::test_complex[+inf+1.23e7j-(inf+12300000j)] pytest tests/parser/test_expressions.py::test_complex[special_case_7]
[0] https://docs.pytest.org/en/7.1.x/example/parametrize.html#di...I'm hoping this PEP Proposal for lazy imports might solve things: https://peps.python.org/pep-0690/
Try profiling pytest with something like pyinstrument[1] and make sure you invoke it from python directly[2] with `--collect-only` as an argument. You'll essentially then just be profiling the import overheads before tests are ran.
[1] - https://pyinstrument.readthedocs.io/en/latest/guide.html#pro...
[2] - https://pyinstrument.readthedocs.io/en/latest/guide.html#pro...
If you’ve got a large project that’s been going for a while, squashing migrations helps too. It can be a bit tricky to do this though.
https://plugins.jetbrains.com/plugin/21206-codiumai--meaning...
https://marketplace.visualstudio.com/items?itemName=Codium.c...
Also, content takes time to make. If you want to charge for it I think that's completely fine. It's not like it must be our based.
Only if you have unlimited potential work for that rate, and are actually billing for contracted work, not by the hour.
In most scenarios, you will have paid for the book yourself, spent some time reading it (hopefully billable hours) and there is a real chance increased knowledge actually leads to more work, as you now have a more in depth understanding of the topic.
To me it all reads like the typing speed vs productivity essays. Yes, you should not be hindered by hunt and peck style typing interfering with your train of thoughts. But never seen someone who writes actual loc or actual content (not casual e-mail replies) where typing speed drives productivity in any way.