patternpythonMinor
Do my unit tests follow best practices?
Viewed 0 times
testspracticesfollowunitbest
Problem
I've watched quite a few videos and read a couple of articles on unit testing and I've tried my best to make this test case as good as possible. In which areas can it still be improved?
I should probably add that SM2Scheduler is an implementation of the SM2 algorithm, which is used in flashcard software such as Anki and Mnemosyne.
```
import datetime
import os
import pathlib
import sys
import unittest
# Resolve the relative import error
path_to_this_file = pathlib.Path(__file__)
path_to_srsalgorithms = path_to_this_file.parents[1]
sys.path.append(str(path_to_srsalgorithms))
from sm2 import SM2Scheduler
class TestSM2Scheduler(unittest.TestCase):
def setUp(self):
self.scheduler = SM2Scheduler()
def test_init_last_review_date_is_today(self):
last_review_date = self.scheduler.last_review_date
today = datetime.date.today()
self.assertEqual(last_review_date, today)
def test_init_new_card_is_to_be_seen_today(self):
days_until_next_review = self.scheduler.days_until_next_review
self.assertEqual(days_until_next_review, 0)
def test_init_new_card_has_not_been_reviewed_yet(self):
number_of_repetitions = self.scheduler.number_of_repetitions
self.assertEqual(number_of_repetitions, 0)
def test_init_default_easiness_factor_is_2_point_5(self):
easiness_factor = self.scheduler.easiness_factor
self.assertEqual(easiness_factor, 2.5)
def test_feed_last_review_date_is_updated_after_repetition(self):
self.scheduler.feed(5)
last_review_date = self.scheduler.last_review_date
today = datetime.date.today()
self.assertEqual(last_review_date, today)
def test_feed_number_of_repetitions_is_set_to_0_if_card_rating_is_below_3(self):
self.scheduler.feed(2)
number_of_repetitions = self.scheduler.number_of_repetitions
self.assertEqual(number_of_repetitions, 0)
def test_feed_number_of_repetitions_is_incremented_if_card_rating
I should probably add that SM2Scheduler is an implementation of the SM2 algorithm, which is used in flashcard software such as Anki and Mnemosyne.
```
import datetime
import os
import pathlib
import sys
import unittest
# Resolve the relative import error
path_to_this_file = pathlib.Path(__file__)
path_to_srsalgorithms = path_to_this_file.parents[1]
sys.path.append(str(path_to_srsalgorithms))
from sm2 import SM2Scheduler
class TestSM2Scheduler(unittest.TestCase):
def setUp(self):
self.scheduler = SM2Scheduler()
def test_init_last_review_date_is_today(self):
last_review_date = self.scheduler.last_review_date
today = datetime.date.today()
self.assertEqual(last_review_date, today)
def test_init_new_card_is_to_be_seen_today(self):
days_until_next_review = self.scheduler.days_until_next_review
self.assertEqual(days_until_next_review, 0)
def test_init_new_card_has_not_been_reviewed_yet(self):
number_of_repetitions = self.scheduler.number_of_repetitions
self.assertEqual(number_of_repetitions, 0)
def test_init_default_easiness_factor_is_2_point_5(self):
easiness_factor = self.scheduler.easiness_factor
self.assertEqual(easiness_factor, 2.5)
def test_feed_last_review_date_is_updated_after_repetition(self):
self.scheduler.feed(5)
last_review_date = self.scheduler.last_review_date
today = datetime.date.today()
self.assertEqual(last_review_date, today)
def test_feed_number_of_repetitions_is_set_to_0_if_card_rating_is_below_3(self):
self.scheduler.feed(2)
number_of_repetitions = self.scheduler.number_of_repetitions
self.assertEqual(number_of_repetitions, 0)
def test_feed_number_of_repetitions_is_incremented_if_card_rating
Solution
Drop one-time use local variables and use libraries such as PyHamcrest to improve assertion readability.
Compare
with the original
BTW, does
Compare
def test_init_last_review_date_is_today(self):
assert_that(self.scheduler.last_review_date, is(datetime.date.today))with the original
def test_init_last_review_date_is_today(self):
self.assertEqual(self.scheduler.last_review_date, datetime.date.today())BTW, does
assertEqual follow xUnit conventions and put the expected value before the actual value? If so, your assertion error messages will be backwards: "expected 'foo' but got 'bar'" when testing the stub method bar() { return "foo"; }Code Snippets
def test_init_last_review_date_is_today(self):
assert_that(self.scheduler.last_review_date, is(datetime.date.today))def test_init_last_review_date_is_today(self):
self.assertEqual(self.scheduler.last_review_date, datetime.date.today())Context
StackExchange Code Review Q#45457, answer score: 3
Revisions (0)
No revisions yet.