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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:27:33+00:00 2026-06-01T01:27:33+00:00

For a unit test I want to use the Range attribute from NUnit to

  • 0

For a unit test I want to use the Range attribute from NUnit to test inputs to a function in a range. The lower and upper limits of this range are coded into constant properties of a (Singleton pattern) class. I would like to specify the starting point and end point of the Range attribute with the class properties, something like this:

[Test]
public void sometest([Range(MyClass.LOWER_LIMIT,MyClass.UPPER_LIMIT)] int var)
{
    //Do something and assertive with the nice variable
}

However, this approach does not work. Although it is not clear from the documentation itself, it seems that the Range attribute must be provided constant variables. While my class constants are static properties with only get defined, this does capture a constant variable.

I posted and answer to this question, but is this really the way to set the range parameters based on class constant in NUnit? Or is there a more elegant solution?

  • 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-01T01:27:35+00:00Added an answer on June 1, 2026 at 1:27 am

    The following example demonstrates how one can use (constant)properties from a class as values used with the Range attribute from NUnit.

    const int LO_LIM = 1;
    const int HI_LIM = 10;
    
    [Test]
    public void assertConstantsCorrect()
    {
        //Will fail if constants change during development!
        Assert.AreEqual(MyClass.LOWER_LIMIT,LO_LIM);
        Assert.AreEqual(MyClass.UPPER_LIMIT,HI_LIM);
    }
    
    [Test]
    public void sometest([Range(LO_LIM,HI_LIM)] int var)
    {
        //Do test
    }
    

    The first step is to define constants in your test class, as the Range attribute only works with constants. These constants take the same values as the constants defined in the properties of your class.

    Second is a Test created to verify that they correspond. If at a later date and time the constants in MyClass change, the failure in this test will notify you of this change. Take note that if this test does not pass, any other test using those constants can be regarded as invalid as they rely on false asumptions!

    Lasty are your actual tests that use those values in the [Range( start, end)] clause.

    Alternatively, you can also make use the [TestFixtureSetUp] attribute instead of the [Test] attribute for the assertConstantsCorrect() method to make all tests in the fixture fail in case the assertConstantsCorrect() fails.
    Yet another alternative is to make a custom attribute to work for specific methods you as programmer annotate and make those methods fail when assertConstantsCorrect() fails.

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

Sidebar

Related Questions

Use case is this: I want to unit test (in browser, QUnit or something
I want to Unit Test my application which use MSMQ but i found no
I want to unit test a RESTful interface written with Apache CXF. I use
I want to unit test a Java application that fetches mails from an email
I want to use mstest.exe to run my unit test on build server, but
I am using .NET 4, NUnit and Rhino mocks. I want to unit test
I have some kind of test data and want to create a unit test
I want to write a unit test for a Django manage.py command that does
I want to create a unit test for integration testing. What class should I
I want to create a SQL Select to do a unit test in MS

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.