I understand that, as an option, it is possible in the test class itself to create a nested class that will get access to all private external members. And even it is possible to protect all this with the #if DEBUGER ... #endif , but I would not like to mix the test and testing code. I would very much like to make tests in a separate project, as was done by default. At the same time, I would not like to refer to hidden fields every time through Reflection calls, even if they are rendered into classes, methods.

How was this intended to be done by Microsoft? Are there any recommendations and other developments in relation to MSTest?

  • one
    private properties and methods are auxiliary for public ones, therefore, in a public test, you implicitly test private ones. In addition, since you mentioned TDD, the correct operation of the public interface is essentially important, and how you implemented it inside it, no one except you should care. But for testing protected methods, which, although not a public interface, may have similar requirements, since the correct operation of derived classes depends on them, you can test using a special derived class. - rdorn
  • In part, this is true, but sometimes private logic can be so complicated that it is impossible to test it properly through public ones. There are some subtle places that only a programmer knows who has written the code and that he may want to check them. This is an option to the main tests of the public interface. Probably all the same Reflection is a good idea, but I would not like to reinvent the wheel from scratch. - Roman Yermolin
  • 2
    If everything is so difficult, then it is worth thinking about a possible design error. In an extreme case, you can take out particularly complex logic to a separate helper class and test it - rdorn
  • In order not to deviate from the topic, I propose not to discuss the question “why”, but to discuss the question “how” If you suddenly need to test private, then how to do it without interfering with the project. - Roman Yermolin
  • And the question "how" you have already answered - reflection, there is no other way to "legally" get access to private members. - rdorn

1 answer 1

PrivateObject

Only suitable for MSTest Itself did not work with this, but if someone comes in handy:

 public class Account { public decimal InterestRate { get; private set; } private decimal _balance; private int _accountId; public Account(int accountId) { _accountId = accountId; } public decimal GetBalanceWithInterest() { return _balance + GetInterest(); } //закрытый метод private decimal GetInterest() { return _balance*InterestRate; } } [TestMethod] public void GetInterest_ReturnsExpectedInterest() { decimal balance = 1000; decimal interestRate = (decimal) 0.05; decimal expectedInterest = 50; var acc = new Account(1000); var privateObject = new PrivateObject(acc); privateObject.SetField("_balance", balance); privateObject.SetProperty("InterestRate", interestRate); var interest = privateObject.Invoke("GetInterest"); Assert.AreEqual(interest, expectedInterest); }