1

I'm trying to unit test my minesweeper game made in python. I am starting small with just 1 test on 1 definition, but it runs the entire code of which I should be testing a very minor part. Unit testing code:

import unittest
from minesweeper import setupgrid

class Testmyfunctions(unittest.TestCase):
    def test_setup(self):
        self.assertTrue(setupgrid(9, [], 10))


if __name__ == '__main__' :
    unittest.main(exit=False)

And this is the function it should be checking:

import random, re, time
from string import ascii_lowercase


def setupgrid(gridsize, start, numberofmines):
    emptygrid = [['0' for i in range(gridsize)] for i in range(gridsize)]
    mines = getmines(emptygrid, start, numberofmines)
    for i, j in mines:
        emptygrid[i][j] = 'X'
    grid = getnumbers(emptygrid)
    return (grid, mines)

The default values for emptygrid and numberofmines are, respectively, 9 and 10 and the value for start should be empty, hence the [].

L. de Boer
  • 88
  • 1
  • 11
  • Your indentation is broken, either in your actual code or the code posted here. – chepner Jan 12 '17 at 19:41
  • @chepner I'm sorry, can you explain what you mean with 'indentation'? I'm both a noobie at programming and not a natively english speaker – L. de Boer Jan 12 '17 at 19:43
  • The code you have posted here is not indented correctly; for example, none of the lines that should be in the body of `setupgrid` are indented, while the body of the `for` loop *is* properly indented. – chepner Jan 12 '17 at 19:46
  • @chepner you mean that I am using to many breaklines in code? – L. de Boer Jan 12 '17 at 19:53
  • 2
    The indentation is the empty space on the line before the text. Python uses the indentation of a line to determine the what's inside code blocks. For example, after your `def setupgrid(gridsize, start, numberofmines):` line, all the lines of code that are part of that function should be further to the right than that line, to indicate that they are the body of the `setupgrid` function. I suspect that this is just a case of copy-pasting meesing up your code though. – Patrick Haugh Jan 12 '17 at 20:00
  • @PatrickHaugh Thank you so much for explaining. It is indeed a mess-up on the part of copy-paste. Do you have any other idea that might cause the issue? – L. de Boer Jan 12 '17 at 20:08
  • I think you are calling `setupgrid` wrong in your unit test. Why are the arguments in a list? shouldn't they be like `setupgrid(9, [], 10)` ? – Patrick Haugh Jan 12 '17 at 20:20
  • @PatrickHaugh You are right, I fixed it, but that wasn't the thing that caused the issues I'm afraid. Still, thank you for your time and helping me improve. – L. de Boer Jan 12 '17 at 20:37
  • Possible duplicate of [Why is Python running my module when I import it, and how do I stop it?](http://stackoverflow.com/questions/6523791/why-is-python-running-my-module-when-i-import-it-and-how-do-i-stop-it) – fuglede Jan 12 '17 at 21:09
  • 1
    Could you please describe/show what code is getting run unexpectedly when you run your unittest? Also, as an aside, your `test_setup` method would be more informative when run and when someone is looking at your tests, if you asserted based on exepected return value of your `setupgrid()` function. As it is, any return value that is "truthy", i.e., where `bool(value)` returns `True`, will cause your test to pass. For example, even if your setup method returns `(None, None)`, this will pass your test, even though I would assume that this return value is not expected. – elethan Jan 12 '17 at 21:57

0 Answers0