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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:20:16+00:00 2026-06-17T04:20:16+00:00

I read somewhere that each test must test only one thing. But is grouping

  • 0

I read somewhere that each test must test only one thing. But is grouping similar behavior allowed in the good practices handbook? I’m currently writing some tests (C# with NUnit) and bellow is an example of what I’m facing:

[TearDown]
public void Cleanup()
{
    Hotkeys.UnregisterAllLocals();
    Hotkeys.UnregisterAllGlobals();
}

[Test]
public void KeyOrderDoesNotMatter()
{
    Hotkeys.RegisterGlobal("Ctrl+Alt+P", delegate { });
    Assert.That(Hotkeys.IsRegisteredGlobal("Alt+P+Ctrl"), Is.True);
}

[Test]
public void KeyCaseDoesNotMatter()
{
    Hotkeys.RegisterGlobal("Ctrl+Alt+P", delegate { });
    Assert.That(Hotkeys.IsRegisteredGlobal("ctrl+alt+p"), Is.True);
}

[Test]
public void KeySpacesDoesNotMatter()
{
    Hotkeys.RegisterGlobal("Ctrl+Alt+P", delegate { });
    Assert.That(Hotkeys.IsRegisteredGlobal("Ctrl + Alt + P"), Is.True);
}

Grouped, they would become:

[TearDown]
public void Cleanup()
{
    Hotkeys.UnregisterAllLocals();
    Hotkeys.UnregisterAllGlobals();
}

[Test]
public void KeyIsNotStrict()
{
    // order
    Hotkeys.RegisterGlobal("Ctrl+Alt+A", delegate { });
    Assert.That(Hotkeys.IsRegisteredGlobal("Alt+A+Ctrl"), Is.True);

    // whitespace
    Hotkeys.RegisterGlobal("Ctrl+Alt+B", delegate { });
    Assert.That(Hotkeys.IsRegisteredGlobal("Ctrl + Alt + B"), Is.True);

    // case
    Hotkeys.RegisterGlobal("Ctrl+Alt+C", delegate { });
    Assert.That(Hotkeys.IsRegisteredGlobal("ctrl+alt+c"), Is.True);
}

So what is the best practice (if one exists) and why?

obs: i’m relatively new to unit testing…

  • 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-17T04:20:17+00:00Added an answer on June 17, 2026 at 4:20 am

    Note: I am not a C# programmer.

    On the one hand, you’ve got “do only one thing in a test”. On the other you have the DRY Principle. You’re being asked which to violate. This depends on how badly you’re violating each of them, how much benefit you get out of the violation, and why those rules exist in the first place.

    Your grouped solution is not ideal, because it still repeats itself. If instead you did this…

    [TearDown]
    public void Cleanup()
    {
        Hotkeys.UnregisterAllLocals();
        Hotkeys.UnregisterAllGlobals();
    }
    
    [Test]
    public void IsRegisteredGlobal_InputNormalization()
    {
        Hotkeys.RegisterGlobal("Ctrl+Alt+P", delegate { });
    
        Assert.IsTrue(Hotkeys.IsRegisteredGlobal("Alt+P+Ctrl"),     "order independent");
        Assert.IsTrue(Hotkeys.IsRegisteredGlobal("ctrl+alt+p"),     "case insensitive");
        Assert.IsTrue(Hotkeys.IsRegisteredGlobal("Ctrl + Alt + P"), "whitespace independent");
    }
    

    Then you’re not violating DRY and you’re hardly scuffing “do only one thing in a test”. Its still doing one “thing” and that thing is normalizing the input of IsRegisteredGlobal.

    You do just one thing per test in order to isolate them. This makes tests easier to isolate and debug. This doesn’t mean one assert per test. The test above is still doing only one thing, but it is testing it in three very, very similar ways. This is ok. Previously your asserts were explained by the test name. Now they are explained by a message associated with each assert. The reason for failure remains obvious, the I in FIRST.

    Furthermore, if you were to write all your tests with one assert per method and repeat the same code over and over unnecessarily, you not just violate DRY but the repeated code may start to slow things down violating the F in FIRST.

    Is it possible that checking Hotkeys.IsRegisteredGlobal("Alt+P+Ctrl") might cause Hotkeys.IsRegisteredGlobal("ctrl+alt+p") to become true when if you reversed their order it wouldn’t be? Yes. But there’s always a trade off between isolation, speed and convenience. If you suspect one might interfere with another, you should isolate them. I’d say that if you wanted to check there’s no coupling between then you should do that explicitly, in its own test, rather than saddling every test with that burden.

    Yes, its fine to run code and do multiple asserts on it, but always remember it is a balancing act between good code and good tests. Usually the testing wins, but don’t get silly about it.

    I switched it to IsTrue rather than the generic That for compactness, clarity and
    potentially better failure diagnostics. Assert.That( thing, condition ) means you don’t know what you’re testing for until the end. You have to read the whole line, see what the condition is, and read the whole line again in light of what you’re really testing for. Assert.IsTrue tells you up front. Assert.That may be more readable in the complex assertions, but its less readable in the simple ones. It’s ok to use them as appropriate. (Note: Perl programmer)

    It can also potentially produce better failure diagnostics because you’re giving NUnit more information about your intent. It’s not “this matches that” but “this is true” so it can produce a more exact and crafted assert. Though NUnit might also be smart enough to see that you’re testing against Is.True and do that anyway. It doesn’t hurt.

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

Sidebar

Related Questions

I read somewhere that somebody could access a config value during run time but
I think I read somewhere that some modules only have object oriented interfaces (
I read somewhere that each object's instance variables are unique. I'm not sure how
How much RAM is available for use by each app? I read somewhere that
I know about mysql_num_rows ( resource $result ) but I have read somewhere that
I read somewhere that when rendering a lot of 3D objects one could merge
Can some one explain the conceptual difference between both of them. Read somewhere that
I read somewhere that one should never use error conditions as normal program flow.
I read somewhere that the ?: operator in C is slightly different in C++,
I read somewhere that Pattern Matching like that supported by the match/case feature in

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.