Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the testing of methods #1

Open
mwermelinger opened this issue Jan 23, 2024 · 0 comments
Open

Support the testing of methods #1

mwermelinger opened this issue Jan 23, 2024 · 0 comments
Labels
priority: high status: idea Issue not yet fully specified

Comments

@mwermelinger
Copy link
Member

Essays can also be about data structures, e.g. comparing two implementations of the same ADT. Normally, each implementation is a class, so the library should make it easier to test methods, besides functions. Here's one possible suggestion how to go about it:

  • The class must allow parametrised initialisation.
  • The class author must write for each method (at least for the modifiers), one test function that takes an already initialised object of that class.
  • The class author must provide a list of arguments to initialise the class with.
  • Then call a function that runs all tests on all possible instances.
class ArrayStack:
    def __init__(items):
        """Initialise a stack with the given items."""
        ...

    def pop(...):

class LinkedListStack:
    ....

Stack = LinkedListStack | ArrayStack

def test_pop(stack: Stack):
    """Check the `pop` method on the (possibly empty) `stack`."""
    ...

init_stack = [ # sequences of items to push on the stack before testing it
    [],
    "abc",
    (True, False),
    ...
]

for class in (ArrayStack, LinkedListStack):
    test_methods(class, init_stack)

The last line does the following:

for items in init_stack:
    test_pop(ArrayStack(items))
    test_push(ArrayStack(items))
    ...

and then the same for the other stack implementation. More generally, it creates one instance of the class (first argument) for each initialisation argument (second argument), for each method m for which a function named test_m exists.

The question remains on how to detect and report failed tests. pytest introspects the assert statement to report why it fails.

@mwermelinger mwermelinger added the status: idea Issue not yet fully specified label Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: high status: idea Issue not yet fully specified
Projects
None yet
Development

No branches or pull requests

1 participant