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

  • SEARCH
  • Home
  • 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 8881217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:17:59+00:00 2026-06-14T20:17:59+00:00

I’ve written a script that opens up a file, reads the content and does

  • 0

I’ve written a script that opens up a file, reads the content and does some operations and calculations and stores them in sets and dictionaries.

How would I write a unit test for such a thing? My questions specifically are:

  1. Would I test that the file opened?
  2. The file is huge (it’s the unix dictionary file). How would I unit test the calculations? Do I literally have to manually calculate everything and test that the result is right? I have a feeling that this defeats the whole purpose of unit testing. I’m not taking any input through stdin.
  • 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-06-14T20:18:00+00:00Added an answer on June 14, 2026 at 8:18 pm

    That’s not what unit-testing is about!

    1. Your file doesn’t represent an UNIT, so no you don’t test the file or WITH the file!
    2. your unit-test should test every single method of your functions/methods which deals with the a)file-processing b) calculations
    3. it’s not seldom that your unit-tests exceeds the line of code of your units under test.

    Unit-test means (not complete and not the by-the-book definition):

    • minimalistic/atomic – you split your units down to the most basic/simple unit possible; an unit is normally a callable (method, function, callable object)
    • separation of concern – you test ONE and only ONE thing in every single test; if you want to test different conditions of a single unit, you write different tests
    • determinism – you give the unit something to process, with the beforehand knowledge of what it’s result SHOULD be
    • if your unit-under-test needs a specific enviroment you create a fixture/test-setup/mock-up
    • unit-tests are (as a rule of thumb) blazingly fast! if it’s slow check if you violated another point from above
    • if you need to test somethin which violates somethin from above you may have made the next step in testing towards integration-tests
    • you may use unit-test frameworks for not unit-testings, but don’t call it unit-test just because of the use of the unittest-framework

    This guy (Gary Bernhardt) has some interesting practical examples of what testing and unit-testing means.

    Update for some clarifications:

    “1. Would I test that the file opened?”

    Well you could do that, but what would be the “UNIT” for that? Keep in mind, that a test has just two solutions: pass and fail. If your test fails, it should (ideally must) have only one reason for that: Your unit(=function) sucks! But in this case your test can fail, because:
    * the file doesn’t exist
    * is locked
    * is corrupted
    * no file-handles left
    * out of memeory (big file)
    * moon- phase
    and so on.

    so what would a failing (or passing) “unit” test say about your unit? You don’t test your unit alone, but the whole surrounding enviroment with it. That’s more a system-test!
    If you would like to test nontheless for successful file-opening you should at least mock a file.

    “2 … How would I unit test the calculations? Do I literally have to manually calculate everything and test that the result is right?”

    No. You would write test for the corner- and regular-cases and check the expected outcome against the processed one. The amount of tests needed depends on the complexity of your calculations and the exceptions to the rule.

    e.g.:

    def test_negative_factor(self):
       assert result 
    
    def test_discontinuity(self):
       assert raise exception if x == undefined_value
    

    I hope i made myself clearer!

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.