Note that there are some explanatory texts on larger screens.

plurals
  1. POExtracting "extra" docstrings from Python code?
    text
    copied!<p>Python docstrings that immediately follows the declaration of a class or function are placed in the <code>__doc__</code> attribute.</p> <p>The Question: How does one extract additional "internal" docstrings that occur later on in a function?</p> <p>Update: Such literal statements are elided by the compiler. Could I perhaps get to them (and their line number) via the AST?</p> <hr> <p>Why do I ask?</p> <p>I've had a (not fully baked) idea to use such "internal" docstrings to delineate Given/When/Then sections of an Agile Scenario:</p> <pre><code>def test_adding(): """Scenario: Adding two numbers""" adder = Adder() """When I add 2 and 3""" result = adder.add(2, 3) """Then the result is 5""" assert result == 5 </code></pre> <p>By extracting the docstrings, the test-running framework could generate output like this:</p> <pre><code>Scenario: Adding two numbers When I add 2 and 3 (PASS) Then the result is 5 (FAIL) AssertionError Traceback ... </code></pre> <p>I think this would be more concise than the approach taken in <a href="http://pypi.python.org/pypi/behave" rel="nofollow">Behave</a>, <a href="https://github.com/rlisagor/freshen" rel="nofollow">Freshen</a>, <a href="http://packages.python.org/lettuce/tutorial/simple.html" rel="nofollow">Lettuce</a>, <a href="http://pypi.python.org/pypi/pycukes/0.1.2" rel="nofollow">PyCukes</a>, which require defining a separate function for each step. I don't like having to repeat the text of the step as a function name (<code>@When("I add numbers") def add_numbers()</code>). But unlike a plain unit test, the docstrings will add the ability to print out a business-readable scenario for reference.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload