简体   繁体   中英

What are possible ways to unit test this method?

I am currently learning how to use the unittest module. I have a minesweeper like board as an object in the form of the class below:

class Grid:
    ''' class to represent grid sizes. '''
    def __init__(self, width: int, height: int, margin: int, rows: int, columns: int):
        ''' 
        width: int, in pixels
        height: int, in pixels
        margin: int, in pixels
        row: number of squares along y axis
        columns: number of square along x axis
        '''
        self.width = width
        self.height = height
        self.margin = margin
        self.rows = rows
        self.columns = columns
        self.grid = [[0 for _ in range(self.columns)] for _ in range(self.rows)]

    def gridDraw(self):
        '''draws the grid for the game board'''
        for row in range(self.rows):
            for column in range(self.columns):
                color = white.rgb()
                if self.grid[row][column] == 1:
                    color = green.rgb()
                pygame.draw.rect(screen,
                                color,
                                [(self.margin + self.width) * column + self.margin,
                                 (self.margin + self.height) * row + self.margin,
                                 self.width,
                                 self.height])

    def size(self):
        '''returns width, height, margin values '''
        return(self.width, self.height, self.margin, self.rows, self.columns)
    
    def gridVal(self):
        '''returns grid value'''
        return(self.grid)

My question is, how could I go about doing unit tests on this gridDraw method? It doesn't really fall under how I would normally test outputs with assertEqual() functions and the like. My test class is as follows thus far:

class GridTest(unit.TestCase):

    def test_gridDraw(self):

    def test_size(self):

    def test_gridVal(self): 
 

For test_size and test_gridVal , you can simply create a Grid object and call size() or gridVal() , then assertEqual if its return value is as expected.

Here's a pseudocode:

def test_size(self):
  grid = Grid(...)
  assertEqual(grid.size(), ...)

def test_gridVal(self): 
  grid = Grid(...)
  assertEqual(grid.gridVal(), ...)

For test_gridDraw , it's slightly more difficult, you needs to override the pygame class either by adding a fake subclass or mocking it. Then, you sense the fake or mock object to see if its rect() method has been called with expected params.

Alternatively, you can break down gridDraw() into getRects() and make its caller to call pygame.draw.rect so you can separate your code from pygame call and make your code easy-to-test.

Here's a pseudocode of the original code:

# Replace gridDraw with:
def getRects(self):
  rects = []
  for row in range(self.rows):
    for column in range(self.columns):
      ...
      rects.append(...)
  return rects

The caller of gridDraw will instead do this:

for rect in grad.getRects():
  pygame.draw.rect(rect)

Finally, the code of getRects looks like this:

# Instead of test_gridDraw, we do:
def test_getRects(self):
  grid = Grid(...)
  assertEqual(grid.getRects(), ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM