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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:55:50+00:00 2026-05-14T05:55:50+00:00

How to write tests where conditions like the following arise: Test user Input. Test

  • 0

How to write tests where conditions like the following arise:

  1. Test user Input.
  2. Test input read from a file.
  3. Test input read from an environment variable.

It’d be great if someone could show me how to approach the above mentioned scenarios; it’d still be awesome if you could point me to a few docs/articles/blog posts which I could
read.

  • 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-14T05:55:50+00:00Added an answer on May 14, 2026 at 5:55 am

    All three situations you’ve described are where you need to specifically go out of your way to ensure you are using loose coupling in your design.

    Do you really need to unit test Python’s raw_input method? The open method? os.environ.get? No.

    You need to set up your design so that you can substitute other ways of retrieving that input. Then, during your unit tests, you’ll throw in a stub of some sort that doesn’t actually call raw_input or open.

    For instance, your normal code might be something like:

    import os
    def say_hello(input_func):
        name = input_func()
        return "Hello " + name
    
    def prompt_for_name():
        return raw_input("What is your name? ")
    
    print say_hello(prompt_for_name)
    # Normally would pass in methods, but lambdas can be used for brevity
    print say_hello(lambda: open("a.txt").readline())
    print say_hello(lambda: os.environ.get("USER"))
    

    The session looks like:

    What is your name? somebody
    Hello somebody
    Hello [some text]
    
    Hello mark
    

    Then your test will be like:

    def test_say_hello():
        output = say_hello(lambda: "test")
        assert(output == "Hello test")
    

    Keep in mind that you should not have to test a language’s IO facilities (unless you’re the one designing the language, which is a different situation entirely).

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

Sidebar

Related Questions

I'd like to write tests for a suite of Parsec parsers. Here's a simple
Like it or not, occasionally you have have to write tests for classes that
I have an access database file (test.mdb) and I need to write a stored
I'm trying to write a unit test for my FileWatcher class. FileWatcher derives from
I'd like to write a TestNG test to make sure an exception is thrown
I'm trying to write tests for some legacy code with PHPUnit 3.4.9, but it
I write JUnit tests for some Spring MVC Controllers. The initialization of the JUnit
I do write unit tests while writing APIs and core functionalities. But I want
I am trying to write integration tests. Therefore I need the start methods of
I am trying to write contract tests for some widely used interfaces: Along the

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.