Author: Always Learning

  • Pytest

    Testing

    Testing is an essential part of software development that ensures your code works as expected and helps maintain its reliability over time.

    Pytest is a powerful, no-boilerplate-needed testing framework in Python that makes writing simple and scalable test cases easy. Let’s explore how you can leverage Pytest in your Python projects.

    Why Pytest?

    Pytest is a popular testing framework for Python due to its simplicity and flexibility. It supports powerful fixtures, has a rich plugin architecture, and can easily integrate with other testing tools and frameworks.

    With Pytest, writing tests becomes more pythonic and less cumbersome compared to other frameworks like unittest.

    Setting Up Pytest

    First, you need to install Pytest. Simply run:

    pip install pytest

    After installation, you can start writing your test cases.

    Example Project Structure

    Imagine you have a simple Python project with the following structure:

    • my_project/
      • app.py
      • tests/
        • test_app.py

    In app.py, you have a function you want to test. For instance:

    # app.py
    
    def add_numbers(a, b):
        """Add two numbers."""
        return a + b

    Writing Your First Test

    Now, let’s write a test for the add_numbers function in test_app.py.

    # tests/test_app.py
    
    from app import add_numbers
    
    def test_add_numbers():
        assert add_numbers(2, 3) == 5
    

    This test checks if the add_numbers function correctly adds two numbers.

    Running the Tests

    To run your tests, simply execute:

    pytest

    Pytest will automatically discover and run all tests in the tests directory.

    Parametrizing Tests

    One of the powerful features of Pytest is parameterized tests. This allows you to run the same test function with different inputs. For example:

    # tests/test_app.py
    
    import pytest
    from app import add_numbers
    
    @pytest.mark.parametrize("a, b, expected", [(2, 3, 5), (4, 5, 9), (0, 0, 0)])
    def test_add_numbers_parametrized(a, b, expected):
        assert add_numbers(a, b) == expected

    This will run test_add_numbers_parametrized three times with different sets of arguments.

    Handling Exceptions

    Testing exceptions is straightforward with Pytest. Suppose you have a function that raises an exception:

    # app.py
    
    def divide_numbers(a, b):
        if b == 0:
            raise ValueError("Cannot divide by zero")
        return a / b

    You can write a test to ensure the exception is raised correctly:

    # tests/test_app.py
    
    import pytest
    from app import divide_numbers
    
    def test_divide_numbers_exception():
        with pytest.raises(ValueError):
            divide_numbers(10, 0)

    Conclusion

    Testing with Pytest is a vital part of the Python development process. It’s straightforward, flexible, and powerful. By integrating testing into your workflow, you can write more reliable and maintainable code. Remember, a well-tested application is a robust application!

    Other Reading

    4 Techniques for Testing Python Command-Line (CLI) Apps – Real Python

    Get Started — pytest documentation

    Pytest – Starting With Basic Test

    Testing Python Applications with Pytest – Semaphore Tutorial

  • Navigating the Tech World with the ‘7 Why’s’: Insights from Outliers and the Power of Intention

    Introduction

    In the tech industry, where innovation and complexity meet, finding your ‘why’ is not just insightful, it’s crucial for success. This concept becomes even more powerful when intertwined with Malcolm Gladwell’s insights from “Outliers,” particularly his observation about plane crashes resulting from seven consecutive human errors. In this article, we delve into how the ‘7 Whys’ technique, combined with a deliberate and intentional approach, can be transformative for tech professionals.

    The Importance of “Why” in Tech:

    The relentless pace of the tech world demands more than just technical skills; it requires clarity of purpose and direction. Gladwell’s analysis of errors leading to aviation disasters is a poignant reminder of how a lack of clarity can lead to cumulative missteps in technology projects and careers.

    The 7 Why’s Technique

    Originally used in manufacturing to identify root causes, the ‘7 Whys’ is a method of iterative questioning that helps uncover deeper motivations. It aligns perfectly with Gladwell’s concept of compounded errors, highlighting the need to understand the underlying reasons behind our professional actions to avoid potential pitfalls.

    Finding your Why

    Being Deliberate and Intentional

    In tech, every decision and action should be deliberate and intentional. This approach is not just about making choices; it’s about understanding the reasons behind these choices.

    Being deliberate means being aware of the implications of your actions, just as understanding the consecutive errors in a plane crash helps in preventing them. Being intentional is about aligning your actions with your deeper purpose – your ‘why’. This section can explore how a deliberate and intentional approach, combined with the ‘7 Whys’, can lead to more meaningful and impactful work in tech.

    Applying the 7 Whys in Tech

    Consider your engagement in a tech project. Asking ‘Why am I working on this?’ might initially yield a surface-level answer. But as you continue to question, seeking deeper understanding, you might uncover motivations like a desire to innovate for social good or bridge digital divides, leading to more focused and intentional work.

    Conclusion

    In the dynamic world of technology, understanding your ‘why’ and acting deliberately and intentionally can be your guiding stars. The ‘7 Whys’, especially when considered alongside the insights from “Outliers”, offers a path to self-discovery and purpose-driven work.

    As you evolve in your tech career, continually revisiting your ‘why’ and the intent behind your actions will keep you aligned and prevent the ‘crashes’ of professional misdirection.

  • Just a bit of Fun

    https://www.youtube.com/watch?v=sgZFG_cwMTc