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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:34:12+00:00 2026-06-09T12:34:12+00:00

Consider the following sequence of events: You write a function A() that does a

  • 0

Consider the following sequence of events:

  1. You write a function A() that does a unit of work
  2. You write a a test for function A() to make sure no bugs are present
  3. You write function B() that uses function A()
  4. You write a a test for function B() to make sure no bugs are present
    4.1 Tests for function B() cover function A() As a result you have at least 2 tests covering some of the same functionality

Question 1: Was it worth it to write tests for function A() to begin with?

  1. You write lots of code
  2. You write a huge regression test that tests program functionality end-to-end
    6.1 Effectively this single regression test repeats vast majority of tests already written

Question 2: By following these steps code contains many tests that cover the same thing more than once. Is there a technique to avoid this?

ASSUME:

For the purposes of this question please assume that B does 2 things, one of which is A in its entirety

void performLifeChoice() { // B()
  if (timeIsRight) {
     askForPromotion();   // A()
  } else {
     goBackToSchool();
  }
}
  • 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-09T12:34:13+00:00Added an answer on June 9, 2026 at 12:34 pm

    My general answer would be yes, it’s worth writing unit tests for A(), for 2 reasons:

    1) You may end up calling A() from a different context than B(), and in that case, you will be glad to know that A() is working, and avoid test duplication. If you didn’t have a test for A() separately, you would end up re-writing what are effectively the same tests twice, for the equivalent branch of code.

    2) Relatedly, if you don’t test A() individually, you may end up having turtles all the way down. Imagine now that a function C() is calling B() in one of its branches, and D() calls C(), etc… If you follow the path of only testing the higher level function, you’ll end up with an integration test which will have to cover a larger and larger set of pre-conditions. Keeping individual units tested with the smallest possible context allows you in principle to avoid that problem.

    An implicit conclusion of my answer is that if A() will never, ever be called from a place other than B(), it might be worth making A() private, and at that point, testing may become “optional”. One the other hand, keeping that test might still be useful, in that it will help you pinpoint that when B() fails, it’s because you broke A(). In the words of Kent Beck, “if a test fails, how many things could be wrong? the closer the answer is to 1, the more “unit-y” the test is.” – it’s useful to have unit tests which help you pinpoint exactly where in your code things went wrong.

    Now how could you go about testing B(), without in effect replicating the tests of A(), with added pre-conditions?

    This is where Mocking/Stubbing or similar techniques can come into play. If you consider what B() is doing, it really isn’t performing A(), it’s acting as a “coordinator”, piping conditions to various paths. An adequate test, in my opinion, would be “When TimeIsRight, then B() should call A()”, the answer provided by A() is not relevant to B(), it’s the responsibility of A(), which your unit test covers.

    In that frame, what a test on B() should Assert is that A() is called when TimeIsRight, not what A() returns. Depending on the specifics of your code, you may consider making A() “substitutable” within B(), for instance via an interface, or via a function that is injected in place of A().

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

Sidebar

Related Questions

Consider the following sequence of events: A view v_Foo is defined A user-defined function
Consider the following sequence of events public static void main(String[] a) throws IOException {
Consider the following sequence of actual outcomes for a single static branch. T means
Consider following make: all: a b a: echo a exit 1 b: echo b
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider the following code to demonstrate the question: let sequence = Seq.initInfinite (fun _
Consider the following code that automates generation of Boost.MPL type sequences (list or vector).
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider following JavaScript code (tested in Firefox): function f(a) { if (a == undefined)
Consider the following text from the PostgreSQL documentation on binary copying: 11-byte sequence PGCOPY\n\377\r\n\0

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.