How can I write automatic tests for this Python code?

Viewed 39

My script core.py which is found in the folder preprocessing takes a string and cleans it. It is part of a bigger model (see the last import, but it's unimportant). The dict_english, found in app/core/preprocessing/constants, is just a dictionary of uncommon English words that I replace with other words.

import string
from app.core.preprocessing.constants import dict_english
from app.core.generic.step import Step
from typing import Optional
from app.api.model.my_project_parameters import MyProjectParameters

class TextPreprocessingBase(Step[str, str]):
    def process(self, input_value: str, parameters: Optional[MyProjectParameters] = None) -> str:
        input_value = input_value.replace("'", '')
        input_value = input_value.replace("\"", '')
        printable = set(string.printable)
        filter(lambda x: x in printable, input_value)
        new_string=''.join(filter(lambda x: x in printable, input_value))
        return new_string

class TextPreprocessingEnglish(TextPreprocessingBase):
    def process(self, input_value: str, parameters: Optional[MyProjectParameters] = None) -> str:
        process_english = super().process(input_value, parameters)
        for word, initial in dict_english.items():
            process_english = process_english.replace(word.lower(), initial)
        return process_english

It's easy to test:

string_example= """ Random 'text' ✓"""

a = TextPreprocessingEnglish()
output = a.process(string_example)
print(output)

It prints:

Random text

But I want to write some automatic tests. I thought:

import pytest
from app.core.preprocessing.core import TextPreprocessingBase, TextPreprocessingEnglish
class TestEnglishPreprocessing:
    @pytest.fixture(scope='class')
    def english_preprocessing:
    ...

But I'm stuck here. I just want to test my code on several various strings that I manually write. Is it possible to do this like that or do I just write it like the simple test example above?

1 Answers

This sounds like something you could solve by parametrizing a test, for example:

import pytest
from process import TextPreprocessingEnglish


@pytest.mark.parametrize(
    "input,expected",
    [
        (""" Random 'text' ✓""", "Random text"),
        (""" Some other 'text' ✓""", "Some other text"),
    ],
)
def test_process(input, expected):
    a = TextPreprocessingEnglish()
    output = a.process(input)
    assert output == expected
Related