خرید بک لینک

Vote count: 0

Is there an existing test suite I can make use of to test an implementation of IOBase? If possible, I would like to exhaustively test a new implementation, but there are a lot of tests that would need to be manually written.

For instance, say I have a non-seekable, non-writeable stream. I want to validate that my implementation of read is compliant and that all other methods raise the right kinds of errors. Such a class might look like this:

import io


class NonSeekableReader(io.RawIOBase):
    def __init__(self, b=b''):
        super(NonSeekableReader, self).__init__()
        self._data = io.BytesIO(b)

    def seekable(self):
        retu False

    def writable(self):
        retu False

    def readable(self):
        retu True

    def read(self, n=-1):
        retu self._data.read(n)

Maybe read works as I initially expect, but maybe there are edge cases I would miss with a more complicated implementation. Experimentation has shown that the docs are at the very least confusing as to what expected behavior is.

Based on the python docs I can see that by defining writeable to retu False, write should raise an OSError (in Python 3.5). This is actually not true, it will raise NotImplementedError when inheriting from RawIOBase or AttributeError when inheriting from IOBase. This behavior is not duplicated in seekable, i.e. calling .tell() raises the correct error. To fix the issue, I define write like so:

def write(self, b):
    raise io.UnsupportedOperation("write")

It is worth noting that a file opened with 'r' produces the expected error class.

Since there is no general guidance on implementing these interfaces (that I have found) beyond the linked doc page, I would not have necessarily even thought to check for the issue. A pre-existing test suite would have solved that problem. Or otherwise a description of the necessary tests or edge cases would have guided me around these issues.

Does such guidance exist somewhere that I simply have not yet found?

asked 24 secs ago

برچسب: نویسنده: استخدام کار تاريخ: جمعه 18 تير 1395 ساعت: 6:27

صفحه بندی