Building a small reusable package connects a src/ package layout, pyproject.
Understanding the Concept
Building a small reusable package connects a src/ package layout, pyproject.toml build metadata, unit tests, virtual environments and an isolated wheel build. Test the installed wheel, not only a working directory import.
Typical project files: pyproject.toml, src/sample_package/__init__.py, src/sample_package/pricing.py, and tests/test_pricing.py. Commands after installing build: python -m unittest discover; python -m build; then test installing the wheel in a fresh environment.
How this fits into a real test suite
A reliable test starts with a specific expected behavior. Keep the fixture small, arrange the input, call the real code, and assert a result the reader can verify. Add one boundary or failure case so the test does not pass only for a convenient example. In a larger project, run the same test automatically after each change.
Tests are repeatable experiments. Use temporary resources and injected dependencies where possible; when the environment is part of the requirement, add a separate integration test rather than weakening the unit test.
Worked Example
Read the code from the first line to the last. Identify which values are inputs, which operation is being demonstrated and what should happen before running it.
def price_with_tax(subtotal: float, tax_rate: float) -> float:
"""Return a subtotal with a nonnegative tax rate."""
if subtotal < 0 or tax_rate < 0:
raise ValueError('values must not be negative')
return round(subtotal * (1 + tax_rate), 2)
print(price_with_tax(25.0, 0.08))
Walkthrough: What Each Part Does
- The definition price_with_tax creates reusable behavior. Its body runs when called, awaited or instantiated; defining it alone does not run the body of an ordinary function.
- The final check compares actual behavior with an explicit expectation. A successful test tells you that this particular case passed, not that every possible input works.
This demonstration focuses on one practical behavior. Before changing several lines, vary one input or one configuration choice and check whether the result changes for the reason you expected.
The example uses terminal, platform, external tool or process behavior that was not exercised automatically. Run it only in a suitable environment after reviewing the shown permissions and paths.
Important Behavior and Edge Cases
Publishing before checking the built distribution or accidentally importing the source tree instead of the installed wheel.
Typical project files: pyproject.toml, src/sample_package/__init__.py, src/sample_package/pricing.py, and tests/test_pricing.py. Commands after installing build: python -m unittest discover; python -m build; then test installing the wheel in a fresh environment.
Practice Exercise
Package a tiny price calculator, run its test suite and inspect the wheel contents.
Write down the result you expect before running your changed code. Check at least one normal case and one boundary or failure case. If your example starts workers or opens resources, verify how they are cleaned up.
Further reading: official documentation and API reference.
Common Beginner Mistakes
- Publishing before checking the built distribution or accidentally importing the source tree instead of the installed wheel.
- Writing assertions that check implementation details but fail to validate the behavior that matters to the caller.
Key Takeaways
- Building a small reusable package connects a src/ package layout, pyproject.
- Typical project files: pyproject.toml, src/sample_package/__init__.py, src/sample_package/pricing.py, and tests/test_pricing.py. Commands after installing build: python -m unittest discover; python -m build; then test installing the wheel in a fresh environment.
- Practice: Package a tiny price calculator, run its test suite and inspect the wheel contents.