koaning / calmcode-feedback

A repo to collect issues with calmcode.io
12 stars 0 forks source link

PYTEST - temporary files and directories within unit test itself #124

Open petrhrobar opened 2 years ago

petrhrobar commented 2 years ago

Hey Vincent,

I really enjoyed the content you made for PYTEST framework, which I really like.

I wanted to ask you/recommend another content within pytest framework, which I haven't been able to google a lot of information about.

How would you go about writing some unit test function that will test if some function creates a directory and then while performing the unit test itself, the directory/file created by unittest would be deleted.

koaning commented 2 years ago

Ah yeah, that's advanced pytest content.

The trick here is to google for "fixtures". You can have pytest re-use a function that can generate assets for a test. One of these assets is a temporary directory, where you can create/delete files to your heats content.

There's more info on that on the docs.

koaning commented 2 years ago

I've started planning for a "pytest tricks" course that will highlight this more.

petrhrobar commented 2 years ago

Hey Vincent, Great to hear that as I believe this is gonna be a very useful course.

A nice extra feature I have also started to use is to automatically run unit-test in a pre-commit hook. This is a badass thing and one just needs to modify the precommit.yml file by e.g.:

-   repo: local
    hooks:
      - id: pytest-check
        name: pytest-check
        entry: pytest
        language: system
        pass_filenames: false
        always_run: true

Cheers

koaning commented 2 years ago

That hook seems new!

Ironically, the pre-commit course mentions that pytest isnt supported and why that might be a good thing.

Pytest runs can take a really long time, which is why they're not always a good choice for a hook. I'll review the library though, as the video may be due for an update!