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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:17:55+00:00 2026-06-17T13:17:55+00:00

Let’s assume I have a class with some similar properties: public string First {

  • 0

Let’s assume I have a class with some similar properties:

public string First { get; set; }
public string Second { get; set; }
public string Third { get; set; }

I want to test them in the same way in my tests… So I write:

[Test]
public void TestFirst()
{
    // Asserting strings here
}

Is there a way to avoid creating three Tests (one for First, one for Second and one for Third)?

I’m looking for something like [Values(First, Second, Third)], so i can then write one test that will iterate through the properties.

Cheers, and thanks in advance 🙂

  • 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-17T13:17:57+00:00Added an answer on June 17, 2026 at 1:17 pm

    Thanks all for your answers and help. Learned plenty of things.

    Here’s what I’ve ended up doing. I’ve used reflection to get all the string properties, and then set to a value, check value is set, set to null, check to see it returns an empty string (logic in property’s getter).

    [Test]
    public void Test_AllStringProperties()
    {
        // Linq query to get a list containing all string properties
        var string_props= (from prop in bkvm.GetType()
                                .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                          where
                            prop.PropertyType == typeof(string) &&
                            prop.CanWrite && prop.CanRead
                          select prop).ToList();
    
        string_props.ForEach(p =>{
                                     // Set value of property to a different string
                                     string set_val = string.Format("Setting [{0}] to: \"Testing string\".", p.Name);
                                     p.SetValue(bkvm, "Testing string", null);
                                     Debug.WriteLine(set_val);
                                     // Assert it was set correctly
                                     Assert.AreEqual("Testing string", p.GetValue(bkvm, null));
    
                                     // Set property to null
                                     p.SetValue(bkvm,null,null);
                                     set_val = string.Format("Setting [{0}] to null. Should yield an empty string.", p.Name);
                                     Debug.WriteLine(set_val);
                                     // Assert it returns an empty string.
                                     Assert.AreEqual(string.Empty,p.GetValue(bkvm, null));
                                 }
            );
    }
    

    This way I don’t need to be concerned if someone adds a property, since it’ll be checked automatically, without me needing to update the test code (as you might guess, not everybody updates nor writes tests 🙂

    Any comments on this solution will be welcomed.

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

Sidebar

Related Questions

Let's say that I have classes like this: public class Parent { public int
Let's say you have a class, with certain properties, and that you tried your
Let's assume I have the following typedef: typedef void (^myBlock)(id); And I have some
Let me try again.. I have two classes: class OrdenO { public int id_orden_O
Let me explain best with an example. Say you have node class that can
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I have a custom class CustomClass, and I have a collection deriving
Let's say I have the following structure: abstract class Hand {} class Rock extends
Let's say I have pseudocode like this: main() { BOOL b = get_bool_from_environment(); //get
Let's say for a moment that I have the following module in python: class

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.