2016-11-28 51 views
-1

我想写这个代码的单元测试,但我不知道如何开始。 我需要为所有函数编写单元测试,或者我可以传递一些函数吗?在我看来,我写单元测试generate_idadd_noteremove_noteedit_note,是否足够呢?单元测试的第一步

import json 

def get_notes_data(file_name): 
    with open(file_name, 'r') as open_file: 
     return json.loads(open_file.read(), encoding='utf-8') 


notes = get_notes_data('notes_data/notes.json') 

def print_column(column_name): 
    list_of_notes = [note for note in notes if note['board'] == column_name] 
    return [note['message'] for note in notes if note['board'] == column_name] 

#lista = print_column('to do') 

def print_data(): 
    all_columns = [note['board'] for note in notes] 
    columns = set(all_columns) 
    for column in columns: 
     print column 
     print '\n' 
     print print_column(column) 
     print '\n \n' 


def generate_id(): 
    max_id = 0 
    for note in get_notes_data('notes_data/notes.json'): 
     if note['id'] > max_id: 
      max_id = note['id'] 
    return max_id + 1 

def save_notes(file_name, notes): 
    with open(file_name, 'w') as notes_file: 
     json.dump(notes, notes_file, indent=4) 

def add_note(column_name, note_message, notes): 
    note_data = {"board" : column_name, 
      "message": note_message, 
      "id": generate_id()} 
    notes.append(note_data) 
    save_notes('notes_data/notes.json', notes) 
    return note_data 


def remove_note(note_id, notes): 
    for note in notes: 
     if note['id'] == note_id: 
      notes.pop(notes.index(note)) 
    save_notes('notes_data/notes.json', notes) 


def edit_note(note_id, message, board, notes): 
    changed = False 
    for note in notes: 
     if note['id'] == note_id: 
      note['message'] = message 
      note['board'] = board 
      changed = True 
    if not changed: 
     raise IndexError('Index {0} does not exist'.format(note_id)) 
    save_notes('notes_data/notes.json', notes) 

一些单元测试:

def test_generate_id(self): 
    expected = 10 
    actual = first.generate_id() 
    self.assertEqual(expected, actual) 

def test_add_note(self): 
    column_name = 'to do' 
    note_message = 'message to Damian' 
    idx = first.generate_id() 
    new_note = {"message":note_message, 'id': idx, 'board':column_name} 
    first.add_note(column_name, note_message, TestFirst.data) 
    notes = first.get_notes_data(TestFirst.DATABASE) 
    self.assertIn(new_note, notes) 

def test_remove_note(self): 
    idx = 7 
    notes = first.get_notes_data(TestFirst.DATABASE) 
    for note in notes: 
     if note['id'] == idx: 
      to_remove = note 
    first.remove_note(idx, notes) 
    self.assertNotIn(to_remove, notes) 

def test_edit_note_fail(self): 
    note_id = 99 
    message = "except error" 
    board = "to do" 
    notes = first.get_notes_data(TestFirst.DATABASE) 
    self.assertRaises(IndexError, lambda:first.edit_note(note_id, message, board, notes)) 

回答

1

查找到unittest.TestCase模块。见https://docs.python.org/2/library/unittest.html

你要像做

from unittest import TestCase 

class MyTests(TestCase): 

    def setUp(self): 
     # Do test setups here 
     pass 

    def test_my_method_1(self): 
     pass 

    def test_my_method_2(self): 
     pass 

至于用什么来测试,有我给你两个分支覆盖和线路覆盖各种工具。覆盖百分比因意见而异,但一般的经验法则是拍摄80%的覆盖率。

另外,您应该考虑通过传入参数来让您的函数更容易测试。例如:

def generate_id(): 
    max_id = 0 
    for note in get_notes_data('notes_data/notes.json'): 
     if note['id'] > max_id: 
      max_id = note['id'] 
    return max_id + 1 

可能成为

def generate_id(notes_data): 
    max_id = 0 
    for note in notes_data: 
     if note['id'] > max_id: 
      max_id = note['id'] 
    return max_id + 1 

然后它更容易通过传递一些嘲笑了Notes数据和测试返回值

一个试验来写这种情况下,一个独立的测试对于这可能看起来像:

def test_generate_id(self): 
    notes_data = { 'id': 1 } 
    actual = generate_id(notes_data) 
    expected = 2 
    self.assertEquals(actual, expected) 

您可以运行您的测试与nosetests命令。 http://nose.readthedocs.io/en/latest/

+0

谢谢回答!我写了几个单元测试,如果你想要你可以检查它。 –