Testing Private Members

Unit Testing Private Members with XUnit

Updated: 03 September 2023

Testing Private Members

In order to test the values of private members (if for some reason that’s necessary) you will need to make use of reflection. The following two functions will make it possible to access a private field or property

1
private CastType GetPrivateField<BaseType, CastType>(object obj, string fieldName)
2
{
3
return (CastType) typeof(BaseType)
4
.GetField("_endpoint", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance)
5
.GetValue(obj);
6
}
7
8
private CastType GetPrivateProperty<BaseType, CastType>(object obj, string fieldName)
9
{
10
return (CastType) typeof(BaseType)
11
.GetField(obj, BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance)
12
.GetValue(obj);
13
}

Abastract Testing Class

Additionally the Testing Class can inherit from the following base abstract

1
/// <summary>
2
/// Classes that would like to test private members of class can extend this
3
/// </summary>
4
public abstract class AccessPrivateMemberBase
5
{
6
/// <summary>
7
/// Get a Private Field from the given object
8
/// </summary>
9
/// <typeparam name="BaseType">Type of the Input Object</typeparam>
10
/// <typeparam name="CastType">Type of the Output Field</typeparam>
11
/// <param name="obj"></param>
12
/// <param name="fieldName">Name of the Field to get</param>
13
/// <returns></returns>
14
internal CastType GetPrivateField<BaseType, CastType>(object obj, string fieldName)
15
{
16
return (CastType)typeof(BaseType)
17
.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance)
18
.GetValue(obj);
19
}
20
21
/// <summary>
22
/// Get a Private Property from the given object
23
/// </summary>
24
/// <typeparam name="BaseType">Type of the Input Object</typeparam>
25
/// <typeparam name="CastType">Type of the Output Property</typeparam>
26
/// <param name="obj"></param>
27
/// <param name="propertyName">Name of the Property to get</param>
28
/// <returns></returns>
29
internal CastType GetPrivateProperty<BaseType, CastType>(object obj, string propertyName)
30
{
31
return (CastType)typeof(BaseType)
32
.GetField(propertyName, BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance)
33
.GetValue(obj);
34
}
35
}

And then the testing class simply needs to be defined as:

1
public class MyTestingClass : AccessPrivateMemberBase

Inherited Members

A more generic version of the above is also able to test for members that are in inherited classes (which the above will fail at):

1
/// <summary>
2
/// Get a Private Member from the given object.throws a KeyNotFound
3
/// Exception if the member could not be found
4
/// </summary>
5
/// <typeparam name="BaseType">Type of the Input Object</typeparam>
6
/// <typeparam name="CastType">Type of the Output Field</typeparam>
7
/// <param name="obj"></param>
8
/// <param name="fieldName">Name of the Field to get</param>
9
/// <returns></returns>
10
public static CastType GetPrivateMember<CastType>(
11
object obj, string fieldName, int searchDepth = 1
12
)
13
{
14
BindingFlags bindingFlags = BindingFlags.NonPublic
15
| BindingFlags.GetField
16
| BindingFlags.GetProperty
17
| BindingFlags.Instance;
18
19
CastType result;
20
21
result = (CastType)obj
22
?.GetType()
23
?.GetField(fieldName, bindingFlags)
24
?.GetValue(obj);
25
26
int depth = 1;
27
while (result == null && depth <= searchDepth)
28
{
29
depth ++;
30
result = (CastType)obj
31
?.GetType()
32
?.BaseType
33
?.GetField(fieldName, bindingFlags)
34
?.GetValue(obj);
35
36
}
37
38
if (result == null)
39
throw new KeyNotFoundException("The required member could not be found");
40
41
return result;
42
}