[Python] Testing an untestable Python software [closed]

Discussão em 'Python' iniciado por Stack, Setembro 26, 2024.

  1. Stack

    Stack Membro Participativo

    I am basically facing an Hydra. I just came on board of a new project. Most of the code was written by a single developer. The code is not in production yet and it does the job... until when? There are currently about 1,000 lines of code that weren't at all thought to be testable. The code was written by someone who does not use a modern IDE or formatting and linting tools.

    The code is written in Python 3.11 using the procedural paradigm (no classes). It was also decided to use a "script approach" instead of building of complete package. Each file is alone and does not import other project files. The only imports are external dependencies. So if a function is used in several files, it is duplicated again and again.

    There are basically gigantic functions with code looking like the following example with many network operations and operations done with `asyncio.to_thread(...)`:

    async def schedule_job(context: dict) -> None:
    while True:
    -- 200 lines of code --


    As I had a meeting this morning, I shared my concerns and it appears that nobody in the team knows how to test this program. There is at least one kind of planned tests in the roadmap. These are system tests to ensure that the software meets client requirements delegated to an external company.

    My question: How to test a Python code that was not conceived with testability in mind?

    I have already raised the alarm in order to convince the team that we need tests in our code. Currently, I think about proposing the following steps:


    1. Add tools like a formatter or a linter to stop adding code that violates every possible standards and ensure code consistency between team members


    2. Switch to a package approach to have a clean way of importing modules and remove code duplication


    3. Identify most critical modules and prioritize them.


    4. Separate functions with too many responsabilities into smaller ones


    5. Write unit test cases based on module main features


    6. Automate these test suites with CI

    Honestly, I am not so experienced so any advice, resource or methodology from senior developers will be gold.

    Continue reading...

Compartilhe esta Página