Search code examples
c#unit-testingmstestpex

Why does a TestMethod has access to internal properties?


I am very new to testing, and I ran into a particular scenario where a Test Project's Test Method has access to an internal property. Is this working as designed, or can someone please explain to me why this works?

Snippet from Test Class:

/// <summary>This class contains parameterized unit tests for NWatchSystemConfiguration</summary>
    [PexClass(typeof(NWatchSystemConfiguration))]
    [PexAllowedExceptionFromTypeUnderTest(typeof(InvalidOperationException))]
    [PexAllowedExceptionFromTypeUnderTest(typeof(ArgumentException), AcceptExceptionSubtypes = true)]
    [TestClass]
    public partial class NWatchSystemConfigurationTest
    {
        [TestMethod]
        public void CreateEncryptedPropertyTest()
        {
            const string propertyName = "createdEncryptedProperty";
            const string propertyValue = "testValue";

            const string expected = propertyValue;

            target.CreateProperty(propertyName, propertyValue, true); 

            var actual = target.AppSettings.Settings[propertyName].Value;  // AppSettings is an internal property

            Assert.IsNotNull(actual);
            Assert.AreEqual(expected, actual);
        }
    }

Snippet from class being tested:

public class NWatchSystemConfiguration : NWatchConfigurationBase
    {
        internal AppSettingsSection AppSettings;

        // Output emitted for brevity
    }

Solution

  • if you haven't use InternalsVisibleTo attribute in your AssembnlyInfo.cs you won't be able to access it. Take a look at your assemblyinfo.cs. I think you will find something like [assembly: InternalsVisibleTo("TestAssemblyName")] there.