2016-11-28 49 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

謝謝回答!我寫了幾個單元測試,如果你想要你可以檢查它。 –