this post was submitted on 24 Jul 2023
17 points (100.0% liked)

Python

6350 readers
1 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

πŸ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
πŸ’“ Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS
17
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/[email protected]
 

Where do your unit tests live? Does is vary, depending on project size? Do you like a flat structure, with test files living alongside everything else? Are you a nested folders person?

I'm curious the practice of others, because I still don't feel like I've found a testing directory structure that satisfies me.

top 10 comments
sorted by: hot top controversial new old
[–] [email protected] 6 points 1 year ago (1 children)

For unit tests I usually have a test/ folder next to my src/ folder, that duplicates the folder structure. My brain prefers things being seperate from eachother (resources, source code per language, tests) and this is afaik the only way that you can keep it consistent between different languages (C# for example needs a seperate unit test project)

[–] [email protected] 5 points 1 year ago

In Rust, the tests usually sit right next to the source code, even in the same file. That’s partly because the compiler can just strip the tests from the final binary, and I assume partly just conventional. In Python, you usually want to keep the tests out of the final sdist/wheel, so the setup you described is probably the most common in bigger projects.

[–] [email protected] 5 points 1 year ago

In production!

I kid I kid.

[–] [email protected] 5 points 1 year ago (1 children)

For unit tests, I put them within a folder parallel to the source code. The directory in that folder matches the module hierarchy.

A flat structure can work for extremely small projects, but things start to go sideways as the code base grows.

Putting the test files alongside the source files makes it harder work with from a project management perspective:

  • pytest has to check more files when doing discovery, which makes the tests take longer
  • It is significantly harder to configure mypy to a more lenient set of rules for test code.
  • It is harder to package/deploy only the runtime code

If the project is big enough to have integration tests, I prefer to have those in their own folder like the unit test folder. It is possible to mark tests into different categories, but putting them in a dedicated folder makes it is harder for people to forget to mark an integration test.

[–] [email protected] 1 points 1 year ago

Side note, I hadn't tried mypy until your reply mentioned it and made me curious. I love it, and have already integrated it into my editor and pipeline. Thanks!

[–] [email protected] 3 points 1 year ago* (last edited 1 year ago)

My projects are usually laid out like this at work:

src/
  β€” module_1/
    β€” some_file.py
test/
  β€” module_1/
    β€” some_file_test.py
[–] [email protected] 2 points 1 year ago* (last edited 1 year ago)

I just copy the Java dev structure for this.

Which is basically just a reflection of directories. Someone else mentioned it here.

[–] [email protected] 2 points 1 year ago
[–] [email protected] 1 points 1 year ago

In an iPython terminal if I think of doing some, but I do want to implement something more formal soon.

[–] [email protected] 1 points 1 year ago
load more comments
view more: next β€Ί