Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 574197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:48:19+00:00 2026-05-13T13:48:19+00:00

I’m new to unit tests for my own projects, so this is my first

  • 0

I’m new to unit tests for my own projects, so this is my first attempt to write a unit test from scratch. I’m using python, and the unittest module. The TodoList class being tested here is a wrapper for actual lists, with a few extra methods for stuff like saving to disc. It also defines a few methods for getting items by their ID in the list (which isn’t the same as the list index).

Tests (I’ve cut out a few helper methods, and a good few tests for the sake of not having people to scroll forever):

class TodoListTests(unittest.TestCase):

    def setUp(self):
        self.testdata = open("./testdata.json", "r")
        self.testdata_text = self.testdata.read()
        self.testdata.close()

    def tearDown(self):
        try:
            os.remove("./todo.json")
        except OSError:
            # File not created, no need to delete.
            pass

    def create_todolist_and_safe_list(self):
        self.create_data_file()
        self.todolist = todolist.TodoList("./todo.json")
        self.list = json.loads(self.testdata_text)

    def create_data_file(self):
        datafile = open("./todo.json", "w")
        datafile.write(self.testdata_text)
        datafile.close()

    # Snip out a few more helper methods

    def test_loop(self):
        self.create_todolist_and_safe_list()
        test_list = []
        for item in self.todolist:
        test_list.append(item)

        self.assertEquals(test_list, self.list)


    def test_save(self):    
        self.create_todolist_and_safe_list()
        self.todolist.save()
        newfile_text = self.get_data_file_as_string()
        self.assertEquals(newfile_text, self.testdata_text)

    # Snip out the rest of the tests.

Full link to source

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:48:19+00:00Added an answer on May 13, 2026 at 1:48 pm

    I think that you are going in the right way. But I will send some suggestions;

    • Move the self.testdata.close() from setUp() to the tearDown() function.
    • Surround the others open/close with try/finally blocks. So, if a file didn’t open with success it will be closed.
    
        try:
            file.open()
        finally:
            file.close()
    
    
    • Organize better your test folders. I suggest you to create a folder named _tests and inside this folder you should put the tests module (at your case you only have one module). Then, for each module create a folder with the name of the module and puts the files used by the tests of a module inside this folder.

    To know more about TDD and tests you should read the book Test Driven Development: By Example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.