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

Autogenerate tests from README code examples #44

Open
PFython opened this issue Jan 26, 2021 · 0 comments
Open

Autogenerate tests from README code examples #44

PFython opened this issue Jan 26, 2021 · 0 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@PFython
Copy link
Owner

PFython commented Jan 26, 2021

READMEs will often include code to demonstrate the intended behaviour of a script. A great utility would be to automatically convert code snippets into outline tests, or at least an 80% version where all the tedious formatting and copy/pasting has been avoided.

For example:

README:

## 1. ATTRIBUTE NAMES

>>> x = CleverDict(значение = "znacheniyeh: Russian word for 'value'")
>>> x.значение
"znacheniyeh: Russian word for 'value'"

## 2. ERRORS
>>> x.nonexistent
AttributeError: 'nonexistent'

TEST

class test_README_code_examples:
    def test_ATTRIBUTE_NAMES_1(self):
        """ Code Example 1 from section: 1. ATTRIBUTE NAMES of the README """
        x = CleverDict(значение = "znacheniyeh: Russian word for 'value'")
        assert  x.значение == "znacheniyeh: Russian word for 'value'"
    def test_ATTRIBUTE_NAMES_1(self):
        """ Code Example 1 from section 2. ATTRIBUTE NAMES of the README """
        with pytest.raises(AttributeError):
            x.nonexistent

OUTLINE APPROACH

import pyperclip
lines = pyperclip.paste()
lines1 = [x for x in lines.split("\n") if "    " in x]
lines2 = [x.replace('    >>> x\r', 'assert x == "') for x in lines1]
lines3 = [x.replace('>>> ', '') for x in lines2]
lines4 = ["    "+x for x in lines3]
# macro for inserting "assert ", going to end of line, deleting \n and subsequent 8(?) spaces etc.
pyperclip.copy(lines4)  # or actually append to test.py 
@PFython PFython self-assigned this Jan 30, 2021
@PFython PFython added enhancement New feature or request good first issue Good for newcomers labels Jan 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant