2016-11-28 5 views
-1

私はこのコードの単体テストを書いていますが、どのように起動するのか分かりません。 私はすべての関数の単体テストを書く必要がありますか、それらのうちのいくつかは渡すことができますか?私の意見では、私はのためのユニットテストを書くadd_noteremove_noteedit_noteをgenerate_id、十分にそれはありますか?ユニットテストの最初のステップ

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) 

いくつかのユニットテスト:unittest.TestCaseモジュールに

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

ルック。 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 

そして、それはいくつか出て嘲笑のノートデータを渡し、戻り値

にテストをテストすることによって、このような場合のために分離されたテストを記述する方がはるかに簡単だなれますこれは次のようになります:

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

ありがとうございました!あなたがあなたがそれを確認することができれば、私は少数のユニットテストを書いた。 –

関連する問題