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 42681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:22:34+00:00 2026-05-10T15:22:34+00:00

A common task in programs I’ve been working on lately is modifying a text

  • 0

A common task in programs I’ve been working on lately is modifying a text file in some way. (Hey, I’m on Linux. Everything’s a file. And I do large-scale system admin.)

But the file the code modifies may not exist on my desktop box. And I probably don’t want to modify it if it IS on my desktop.

I’ve read about unit testing in Dive Into Python, and it’s pretty clear what I want to do when testing an app that converts decimal to Roman Numerals (the example in DintoP). The testing is nicely self-contained. You don’t need to verify that the program PRINTS the right thing, you just need to verify that the functions are returning the right output to a given input.

In my case, however, we need to test that the program is modifying its environment correctly. Here’s what I’ve come up with:

1) Create the ‘original’ file in a standard location, perhaps /tmp.

2) Run the function that modifies the file, passing it the path to the file in /tmp.

3) Verify that the file in /tmp was changed correctly; pass/fail unit test accordingly.

This seems kludgy to me. (Gets even kludgier if you want to verify that backup copies of the file are created properly, etc.) Has anyone come up with a better way?

  • 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. 2026-05-10T15:22:35+00:00Added an answer on May 10, 2026 at 3:22 pm

    You’re talking about testing too much at once. If you start trying to attack a testing problem by saying ‘Let’s verify that it modifies its environment correctly’, you’re doomed to failure. Environments have dozens, maybe even millions of potential variations.

    Instead, look at the pieces (‘units’) of your program. For example, are you going to have a function that determines where the files are that have to be written? What are the inputs to that function? Perhaps an environment variable, perhaps some values read from a config file? Test that function, and don’t actually do anything that modifies the filesystem. Don’t pass it ‘realistic’ values, pass it values that are easy to verify against. Make a temporary directory, populate it with files in your test’s setUp method.

    Then test the code that writes the files. Just make sure it’s writing the right contents file contents. Don’t even write to a real filesystem! You don’t need to make ‘fake’ file objects for this, just use Python’s handy StringIO modules; they’re ‘real’ implementations of the ‘file’ interface, they’re just not the ones that your program is actually going to be writing to.

    Ultimately you will have to test the final, everything-is-actually-hooked-up-for-real top-level function that passes the real environment variable and the real config file and puts everything together. But don’t worry about that to get started. For one thing, you will start picking up tricks as you write individual tests for smaller functions and creating test mocks, fakes, and stubs will become second nature to you. For another: even if you can’t quite figure out how to test that one function call, you will have a very high level of confidence that everything which it is calling works perfectly. Also, you’ll notice that test-driven development forces you to make your APIs clearer and more flexible. For example: it’s much easier to test something that calls an open() method on an object that came from somewhere abstract, than to test something that calls os.open on a string that you pass it. The open method is flexible; it can be faked, it can be implemented differently, but a string is a string and os.open doesn’t give you any leeway to catch what methods are called on it.

    You can also build testing tools to make repetitive tasks easy. For example, twisted provides facilities for creating temporary files for testing built right into its testing tool. It’s not uncommon for testing tools or larger projects with their own test libraries to have functionality like this.

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

Sidebar

Ask A Question

Stats

  • Questions 109k
  • Answers 109k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer use a cursor ADDENDUM: [MS SQL cursor example] declare @field1… May 11, 2026 at 9:26 pm
  • Editorial Team
    Editorial Team added an answer Generally speaking, things you want the compiler to inline, or… May 11, 2026 at 9:26 pm
  • Editorial Team
    Editorial Team added an answer In order to break a cipher, cryptanalysts use all the… May 11, 2026 at 9:26 pm

Related Questions

A common task in programs I've been working on lately is modifying a text
Please note - I am not looking for the right way to open/read a
We code in C# using VS2008 SP1. We have a server that runs Team
A common task in programming interviews (not from my experience of interviews though) is
In Silverlight (and I guess WPF) why are the properties x:name x:fieldmodifier x:uid the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.