In this blog post, we’ll delve into two essential practices that contribute to robust code - unit testing and code formatting
Published on August 11, 2023 by Dishie Vinchhi
Smoke tests Unit tests Black Code quality
Maintaining code quality is a cornerstone of software development that not only ensures functionality but also facilitates collaboration and longevity. In this blog post, we’ll delve into two essential practices that contribute to robust code: smoke testing and code formatting. These practices not only enhance code reliability but also streamline development processes and foster a healthier codebase.
Writing code is just the beginning; ensuring that it works as intended is where the real value lies. Smoke testing, a practice that involves running quick, high-level tests on functions, is a crucial step in this direction. Here’s an exploration of how we’ve integrated smoke testing into our workflow:
Consider the function ctransform()
def ctransform(x):
"""Copula transformation (empirical CDF).
Parameters
----------
x : array_like
Array of data. The trial axis should be the last one
Returns
-------
xr : array_like
Empirical CDF value along the last axis of x.
"""
xr = np.argsort(np.argsort(x)).astype(float)
xr += 1.0
xr /= float(xr.shape[-1] + 1)
return xr
The tests written for this function should check the following:
x
The unit test for the same is:
@pytest.mark.parametrize("x", [x1, x2])
def test_ctransform(x):
xr = ctransform(x)
assert isinstance(xr, np.ndarray)
assert xr.shape == x.shape
for row in xr:
for cdf in row:
assert cdf >= 0 and cdf <= 1
Diving deeper into our quest for excellence and quality assurance, we used codecov to obtain the extent to which our unit tests explore our codebase.
Consistent code formatting is more than just aesthetics; it enhances readability, reduces errors, and fosters a cohesive codebase. We’ve taken the extra step of adopting the popular code formatter, Black, to ensure a consistent style throughout our code. Here’s how we achieved this: