6 ms·
Pytest Tips and Tricks
- fud101 4y agoWhy does everyone want money for this kind of content. It's exhausting.
- nerdponx 4y agoWhere is the author asking for money?
- no_carrier 4y agoIt looks like a free article to me?
- jjice 4y agoI mean, generally the ROI on stuff like this is pretty solid. Looks like the book is $36 new. From a quick Google, the average software engineer (who is the group most likely to read and benefit from this book) makes 90k per year. Gross, that's 45 dollars an hour. If I buy this book and it saves me one hour of time writing tests, writing better tests, debugging tests, or whatever, it will have paid for itself. That's obviously not counting reading time, but I believe the point stands. All the time saves after that first hour is gravy on top 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.
- pamelafox 4y agoHis book is great, highly recommend, especially if you like hands-on tutorial style. I am a pytest convert now after spending a decade using unittest.
- fud101 4y agoyeah and there is a million other things we need to know. you want to us buy every book or course that comes along for a million different topics which you don't know if it will be worthwhile before the fact?
- mvdwoord 4y ago"If I buy this book and it saves me one hour of time writing tests, writing better tests, debugging tests, or whatever, it will have paid for itself." 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.
- jjice 4y agoYou're right that it doesn't translate literally to cash, but I've been able to cite increased velocity or point to systems I've designed at work with concepts I learned from similarly process books that allowed me to push for some solid compensation increases and I think the couple hundred dollars in books I purchase a year has allowed me to do that.
- adave 4y agoIts like saying why does an engineer want payment for work done.It's book which are notably hard to write, what your are saying is why is it not free? Imo freeloaders are exhausting and you are free to search for content and release one for free.
- systemvoltage 4y agoI have a personal gripe against Pytest. It extremely implicit, does so many things behind the scenes that it makes learning the library difficult for new comers. I had a hard time. For example, fixtures are passed as arguments in function. You can't use IDE tools to find out where they are passed from. There are dozens of things that go completely against the philosophy of Python: Explicit is better than implicit. Pytest is full of magic, it irritates me. The kind of questions asked on Stack Overflow reflects my personal experience with libraries of this kind. So many people are confused.
- epgui 4y agoI agree with you completely, but... > 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
- uranusjr 4y agoIt is the _Zen_ of Python. Words are naturally elusive, contradiction is harmony.
- epgui 4y agoWhen coming back from a lisp like Clojure, it feels a bit more like a cacophony, but I agree with you in general haha.
- Aperocky 4y ago> 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.
- epgui 4y agoIt depends what's in the pack of comparables... But once you get used to an actually simple language like Clojure or Haskell (neither of which is perfect, but both of which are very very simple) it really is hard to unsee all the accidental complexity of most languages.
- japanuspus 4y agoMy favorite pytest trick: Using jupytext notebooks as tests. 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/ https://jupytext.readthedocs.io/en/latest/ [1]: https://stackoverflow.com/a/56813896/212538 https://stackoverflow.com/a/56813896/212538
- cocoflunchy 4y agoDoes anyone have tips on how to speed up test collection in pytest? In our django app (~5000 tests) it takes about 30s to run a single test with `-k testname` when the actual time spent in the test is on the order of a few hundred milliseconds. It hangs forever on the `collecting ...` phase.
- dmurray 4y agoRun the single test with "pytest tests/path/to/test_file.py::TestClass::test_method" instead of using pytest -k to parse all your test code looking for a method with the right name. 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.
- ssl232 4y agoTo add to this, the test ID pytest prints when a test has had a failure/warning is the same form you can call with pytest to run the test. For example, let's say pytest outputs this: =============================== 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)]
- BerislavLopac 4y agoTo add to that, pytest's parametrized IDs can be fully controlled in the test code [0], so your test can be something like: pytest tests/parser/test_expressions.py::test_complex[special_case_7] [0] https://docs.pytest.org/en/7.1.x/example/parametrize.html#different-options-for-test-ids https://docs.pytest.org/en/7.1.x/example/parametrize.html#di...
- masklinn 4y agoAlso pytest has flags like —lf and —sw to rerun failed tests or tackle test suites with lots of failures.
- petr25102018 4y agoYou can also use my article to learn a bit about testing with Pytest! https://stribny.name/blog/pytest/ https://stribny.name/blog/pytest/
- brianzelip 4y agoA couple recent related podcasts on Pytest with co-authors: - https://testandcode.com/195 https://testandcode.com/195 - https://talkpython.fm/episodes/show/407/pytest-tips-and-tricks-for-better-testing https://talkpython.fm/episodes/show/407/pytest-tips-and-tric...
- itamarcode 4y agoYou can also consider using CodiumAI to create tests right inside your IDE. https://www.codium.ai https://www.codium.ai https://plugins.jetbrains.com/plugin/21206-codiumai--meaningful-tests-in-python-powered-by-testgpt https://plugins.jetbrains.com/plugin/21206-codiumai--meaning... https://marketplace.visualstudio.com/items?itemName=Codium.codium https://marketplace.visualstudio.com/items?itemName=Codium.c...
- zikohh 4y agopytest-repeat for flaky tests!
- skimdesk 4y agoAlso, run your tests in watch mode using entr or pytest-watch [0] for faster feedback. [0] https://pypi.org/project/pytest-watch/ https://pypi.org/project/pytest-watch/