Note that there are some explanatory texts on larger screens.

plurals
  1. POGood Practice for reusing unit test on different functions in Python
    primarykey
    data
    text
    <p>I'm new to unit testing and want to start with the <a href="http://readthedocs.org/docs/nose/en/latest/index.html" rel="nofollow">nose framework</a>. But answers using <a href="http://docs.python.org/library/unittest.html" rel="nofollow">unittest</a>, <a href="http://pytest.org/latest/" rel="nofollow">pytest</a> are welcome too. Also of course, general advice.</p> <p>I think I get the basic concept but I lack practice in setting up <em>good</em> test. I'm also struggling with how to layout the tests. <em>I'm especially unsure on how to approach the case where I want to run a couple of test cases on different function of a module:</em></p> <p>For example: I might have a module called <code>diceroller.py</code> it contains a couple of function on simulating rolling dice, modify and test the results and so on. All functions for rolling dice should run trough the same tests (do they return a list of integers, with the right amount of values, are the values in range, etc.). But some of those should also be run against some additional cases.</p> <p>So I got a subdir <code>test</code> and want to setup my testcode there. How do I approach this?</p> <pre><code># a section of `diceroller.py` def roll_a_dice(sides=6): """Return an integer x where `x &gt;= 1 &lt;= sides`""" return random.randint(1, sides) def roll_dice(sides=6, count=1): """Return a list of integers (most function except this)""" rolls = list() while count: rolls.append(random.randint(1, sides)) count -= 1 return rolls def roll_some_dice(sides=6, count=1, times=1): """Return a list of list containing integers""" some_rolls = list() while times: some_rolls.append(roll_dice(sides, count)) times -= 1 return some_rolls def rolling_dice(sides=6, count=1): """Yielding integers `count` times""" while count: count -= 1 yield random.randint(1, sides) </code></pre> <p><strong>Small update</strong></p> <p>Simeon Visser has a good point. But the code above where just functions too ad some context to my Questions, which just is: <em>How can I (re)use test cases on different functions?</em></p> <p>I guess writing tests like <code>check_xyz</code> and then calling it from <code>test_a</code> and <code>test_b</code> for example, is the easiest solution? Or is this bad practice?</p> <p>The solution from Rik Poggi seems to do exactly what is was trying to accomplish (will play around with it right after typing this). But I kinda have the feeling it "complicates" things to much... probably not on the technical side but it comes as "too much" maybe.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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