0

I am using the test facilities provided by Microsoft.VisualStudio.TestPlatform.TestFramework. I have a test method decorated with [TestMethod]. I want to implement this test for various combinations of parameters using [DataRow]. One of the parameters that the method will need is an enum type declared as internal in another assembly; the assembly in which the enum type is defined gives the assembly containing the unit tests access to its internals via InternalsVisibleTo.

Unit test methods are conventionally public within a public class, so far as I can tell. When it comes to this unit test method, either the test method must be inaccessible from outside the unit test assembly (achieved by making it private or internal, or by making the test class that contains it internal) or the enum must be made public. Making the enum public would be inappropriate, so it would appear correct to make the test method internal.

Are there any possible negative consequences to making the unit test method internal?

This question is doubtless a simple one that has a simple answer. I am asking it because I am having no success at all searching the internet for an answer to it. All I can locate are discussions about whether one should test private methods or not.

Note: If you give a comment or answer that fails to answer my question but instead suggests or implies that it's wrong to test internal members of an assembly using InternalsVisibleTo, then I shall reject your answer out of hand.

Note 2: One possible thing to be done in this case is to use a string parameter instead of the enum parameter, use Enum.Parse in the test method, and pass all of the relevant parameters as strings constructed using the nameof operator. I don't like this, but it might be the least bad workaround.

Hammerite
  • 21,755
  • 6
  • 70
  • 91
  • Depending on your test runner, making the test private could result in it not even being found. But you' d have to try with your tooling. – JSteward Feb 08 '19 at 17:44
  • Seems like something that would be easy to test, if you'll pardon the pun. Take a test method, make it private, run the tests. – Heretic Monkey Feb 08 '19 at 17:45
  • 1
    "Just run the tests" is not a helpful answer. Just because the tests run now, on my machine, doesn't mean that they'll run on the build server. And just because they run now, on the build server, doesn't mean that they'll run on the build server the next time the tooling on the build server is updated. I've recently seen a case where the build server was completely failing to run unit tests that were supposed to be running as part of our overnight build, saying that it could not find any tests in the assembly - and not producing any errors. That assembly was testable just fine on dev machines. – Hammerite Feb 08 '19 at 17:48

2 Answers2

2

At least for MSTest, the test runner in visual studio won't detect methods that aren't public when run. Here's a quick sample.

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1() => Assert.IsTrue(true);

    [TestMethod]
    internal void TestMethod2() => Assert.IsTrue(false);
}

Results are that one test was detected, run, and passed. So I would consider that a major negative consequence to not having the test methods public.

Jonathon Chase
  • 9,396
  • 21
  • 39
  • I accept this on the basis that it demonstrates that it's not safe to assume that tests will run if not public. – Hammerite Feb 09 '19 at 13:37
1

[TestMethod]s have to have public access. Here's Microsoft's documentation on the matter.

The consequence is it will not run if private.

R Y
  • 455
  • 1
  • 3
  • 13
  • 1
    Could you direct me to where on the page you linked it says that [TestMethod]s have to be public? Or quote a relevant extract? – Hammerite Feb 08 '19 at 17:50
  • Unfortunately, not really. All I can point out is that there are no references to `private` in anything there, which implies they don't intend it to be used with `private` methods. – R Y Feb 08 '19 at 18:21