Quick blog post: Mocking async #openai package calls with #pytest
(using monkeypatch and asynchronous generators)
https://blog.pamelafox.org/2023/09/mocking-async-openai-package-calls-with.html
👨💻 How to maintain ten thousand tests and four thousand fixtures? 👉 Find out in the talk "Pytest: The Case for Using Classes" by Mikuláš Poul. https://cz.pycon.org/2023/program/talks/79/
#Pytest #TestAutomation
Dzisiaj testy awscli (1.x) zaczęły nagle się sypał. Oczywiście, `PYTEST_DISABLE_PLUGIN_AUTOLOAD=1` pomogło.
Przy okazji postanowiłem sprawdzić, czy `--forked` jest nadal konieczne. Pamiętam, że kiedyś to dodałem, bo w przeciwnym wypadku testy sypały się przy równoległym uruchamianiu. Dodanie `--forked` spowolniło testy, ale nadal były szybsze niż wykonywane szeregowo.
Okazało się niepotrzebne — albo poprawiono błąd w kodzie, albo problem był powodowany przez inną wtyczkę pytesta. Tak czy siak, `--forked` usunięte i teraz testy przechodzą trzy razy szybciej!
Fed up with writing tests within Pytest for network testing? Check out how to dynamically generate tests using Pytest Parametrization in our previous post: https://www.packetcoders.io/dynamically-generating-tests-with-pytest-parametrization/ #Pytest #NetworkAutomation
Question involving #python #sqlalchemy #factoryboy #postgresql #pytest
Currently, we run end-to-end API tests against a sqlite DB, which takes about 2 seconds. I want to switch to postgres (what we use in prod) but that makes the tests take a lot longer, about 12 seconds.
Is there a way to improve this?
What might be causing the slowness? Where can I look? Is it networking to the docker container? Postgres enforcing constraints? Running flush after every DB fixture? Something else?
#python #sqlalchemy #factoryboy #postgresql #pytest
Today, I rip all of my TestCase & SimpleTestCase tests apart & re-write them for pure #pytest as an edumacational experience.
I also get to figure out what the heck is going on with the latest #factory-boy update & why faker is cranky.
This week is all about breaking stuff & piecing it back together. Good times. #Django
I neglected to mention what the `--durations` flag actually does in #pytest
`--durations 10` will show the 10 slowest test durations. This can be modified with the `--durations-min` flag.
I do realize that I am pretty much rathole-ing on minutae for my little #Django projects; but, I find that it is helping me look closer at 'simple' tests and begin to get more selective on my testing efforts. YMMV.
https://docs.pytest.org/en/latest/how-to/usage.html#profiling-test-execution-duration
Better backup of media files in django-cast + TIL how to test #Django management commands using #pytest - published my #weeknotes for last week: https://wersdoerfer.de/blogs/ephes_blog/weeknotes-2023-08-14/
@brianokken We use https://mattermost.com/ at work.
Have you ever thought to ask the #pytest folks for an extra channel on their discord? Would be a win-win situation. Building a new community from scratch is hard.
Specifically I wanted to add __init__.py files to all my test directories so #pytest wouldn't have test collection errors from files with the same name in different directories. I ran
find tests -type dir ! -name '__pycache__' | xargs -I % sh -c 'touch %/__init__.py'
New blog post: Accessibility snapshot testing for Python Web Apps
https://blog.pamelafox.org/2023/08/accessibility-snapshot-testing-for.html
Zaktualizowałem #Gentoo #Python Guide, i teraz zalecamy `rm -rf` na katalogach, które kolidują z zainstalowanymi paczkami i powodują, że testy nie są w stanie odnaleźć skompilowanych rozszerzeń.
Przedtem zalecaliśmy zmianę katalogu roboczego, ale to było bardziej skomplikowane i czasem powodowało inne problemy (#pytest pomijający pliki konfiguracyjne, testy polegające na względnych ścieżkach).
Dodałem też ostrzeżenie, że brakujące rozszerzenia nie zawsze powodują wystąpienie widocznego błędu.
https://projects.gentoo.org/python/guide/test.html#importerrors-for-c-extensions
Test & Code 205: pytest autouse fixtures - with @falcon
https://testandcode.com/episodes/pytest-autouse-fixtures
We discuss what pytest fixtures are, what autouse fixtures are, and good times to use them
#Python #pytest
#Python devs, can you help? I have a context manager that redirects all stdout/stderr to a pipe (created with os.pipe()). With enough output, it reaches the buffer size and hangs on writing. I only read the whole thing when exiting the context manager.
Any hints on how to handle that? Should I run a reader in a thread? Should I use a named pipe instead (maybe they're not limited by a buffer)? Or maybe a plain temporary file? It seems that's what #pytest's capfd fixture does.
Dzisiejsza przygoda z pytestem.
#pytest zgłasza, że wszystkie testy "async def" nie są uruchamiane, bo nie mam właściwej wtyczki. Tyle że wszystko dzieje się wewnątrz mojego testowego kontenera, który ma przynajmniej kilka wtyczek tego typu, i testy innych paczek nie mają tego problemu!
Okazało się, że muszę podać `--asyncio-mode=auto`.
Nie wpadłbym na to, gdyby nie to, że najpierw podejrzewałem konflikt między wtyczkami i próbowałem PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 z `-p aiohttp`. Wówczas brakująca zależna wtyczka spowodowała powstanie błędu:
INTERNALERROR> ValueError: no option named 'asyncio_mode'
I zadzwoniło we właściwym kościele ;-).
https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=3386db3ead980706cdaaff40510a85bae5734d2b
It's been a while! Here's a new blog post about working with #pytest and #containers:
https://blog.oddbit.com/post/2023-07-15-pytest-and-containers/