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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:21:13+00:00 2026-05-11T08:21:13+00:00

I am working on a lib that I want to release in open source.

  • 0

I am working on a lib that I want to release in open source. I have started writing the tests for the code, and I was wondering how I am suppose to test a property in a .Net object. Lets say I have the following:

  public class Person{     #region variables     private string _name = String.Empty;     private string _surname = String.Empty;     #region properties     public string Name{         get{              return _name;         }     }     public string Surname{         get{             return _surname;         }         set{             _surname = value;         }     } }  

I have two questions related to the code:

  1. How do I Unit test a Property that just has a getter (Like Name in the example)
  2. How do I Unit test a Property with a setter and a getter (Like Surname in the example)

I want to test properties that are that simple because I have already found errors in other code were Itellinsense did the wrong autocomplete and the property was not returning the correct variable.

Update:

I am not talking about simple properties as the one in the example, they do have some logic behind them and are quite hard to debug. Writing a test that uses the setter to test the getter and vice versa is not good because if there is a fail I won’t know which method to blame. I am using properties because they were added as public variables and later more logic had to be added.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T08:21:14+00:00Added an answer on May 11, 2026 at 8:21 am

    How do I Unit test a Property that just has a getter (Like Name in the example)

    Really not so different from testing if you had a setter. you’ll just need to find another way of determining the output. Could be in a ctor, or the result of other setters/operations on the object.

    [Test] public void NamePropTest() {     Person p = new Person();      //Some code here that will set up the Person object     //  so that you know what the name will be      Assert.AreEqual('some known value...', p.Name); } 

    If we had setters for Name and SurName, but only a getter for FullName, the test could look like this:

    [Test] public void NamePropTest() {     Person p = new Person();      p.Name = 'Sean';     p.Surname = 'Penn';      Assert.AreEqual('Sean Penn', p.FullName); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use nose to test an application that I am writing using
I'm working on a C++ project that produces a lib that other teams use.
I'm working with an open-source library and they define a class like so: class
I am working on a Java project that I want to deliver to my
I'm working on an installer for an application that I have ported to OSX,
I have a Ruby on Rails app that I'm working on and I'm having
I have some 3rd party jar files that I want to reference in my
I'm working on a multi-project, and right now I have a structure that resembles
I have a code here: <script type=text/javascript src=<?=base_url()?>lib/jquery-1.6.2.min.js></script> <script type=text/javascript> $('.hover-star').click(function (){ $('.someDiv').text(this.val()); })
I am working on a C++ project that uses Qt (gui lib), VTK (graphics

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.