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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:11:30+00:00 2026-06-10T21:11:30+00:00

The consensus seems to be not to test private methods when doing TDD. Should

  • 0

The consensus seems to be not to test private methods when doing TDD.

Should Private/Protected methods be under unit test?

I keep hitting this same scenario. I have a private method (as an example it switches all options to off). It changes lots of state and is called by a few public methods. The changes that the private method makes to the state will remain the same regardless of which public methods get called (all options get set to off).

What’s the best way to test the functionality of this private method without adding many tests that effectively do the same thing?

btw, I’m using QUnit to test Javascript objects.

Here is an overly simplified version of my class.

http://jsfiddle.net/twistedinferno/UMgAx/

Edit

What I’m really trying ask here is not ‘should I or shouldnt I test private methods’ as that has already been answered and the answer to that is no. I want to know how do I best test each public method bearing in mind that many of the assertions will be the same due to the use of my private method. The same private method gets called by many public methods. Is it ok to have a lot of duplicate assertions to test the change of state that occurs when each one of many public methods call my private method?

new fiddle with tests
http://jsfiddle.net/twistedinferno/JHzWh/

  • 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-10T21:11:32+00:00Added an answer on June 10, 2026 at 9:11 pm

    Test the public methods.

    Or, more generally, test the outwardly-visible interface for the object.

    If the private method is used by the public methods, its use will be tested as a result. But the private method itself, being private, isn’t a concern for anything outside of that object, including the tests.

    These tests shouldn’t know or care that such a private method even exists, let alone whether or not they’re testing it. They should test all public functionality.

    If the many tests are doing the same thing, might that be an indication that there’s unnecessary overlap in the public functionality? Maybe there’s room for re-factoring there? It’s entirely conjecture without seeing any code, of course. Can you provide an example?

    Edit

    The additional assertions duplicate the same keystrokes, but the tests are distinctly different because they’re testing different outward-facing functionality. You can always refactor out the duplicated assertions into a single function that each test calls. Sort of a custom aggregate assertion. As long as the public methods continue to have the same observable (and testable) behavior then that’s fine. As soon as one changes, its test will need to change as well of course.

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

Sidebar

Related Questions

The general consensus these days seems to be that you do not store binary
It seems as though the general consensus of the testing community is to not
I'm not really sure what to call this, because static constructor seems to convey
I'm coming at this from a Node.js perspective where the general consensus seems to
The consensus seems to be that all foreign keys need to have indexes. How
YUI Compressor was the consensus best tool for minimizing, but Closure seems like it
Is there a consensus about the best place to put Python unittests? Should the
Is there any built-in support for that? And if not, is there any consensus
When writing code like this jsLint complains about implied globals: var Test = (function(){
I am aware of fputcsv, but according to this "wontfix" bug fputcsv does not

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.