你如何单元测试不同的类访问级别?

时间:2022-09-25 07:33:22

I admit - I'm a complete novice when it comes to unit testing. I can grasp the concepts easily enough (test one thing, break-fix-test-repeat, etc.), but I'm having a bit of a problem getting my mind around this one...

我承认 - 在单元测试方面,我是一个完整的新手。我可以很容易地掌握这些概念(测试一件事,破解 - 修复 - 测试 - 重复等),但是我有一个问题让我想到这个...

I've been tasked with rewriting a large section of our application, and I've got the class structure down pretty well. We have our test projects mixed right in with the rest of the solution, and all the references are lining up the way we want them to. Unfortunately, there are a few Friend classes that can only be accessed from inside the same namespace. As it stands, the test class is not a member of this namespace, so I cannot get direct access to any of those underlying methods, which REALLY need to be tested.

我的任务是重写我们的大部分应用程序,并且我已经很好地完成了类结构。我们将测试项目与其他解决方案混合在一起,所有参考文献都按照我们希望的方式排列。不幸的是,有一些Friend类只能从同一个命名空间内访问。就目前而言,测试类不是此命名空间的成员,因此我无法直接访问任何真正需要测试的底层方法。

From what I've been reading, I could create a public mockup of the classes in question and test it that way, but I'm concerned that down the road someone will make a change in the production code and not copy it out to the test code, defeating the purpose of testing entirely. Another option would be to change the access level on the classes themselves, but that would involve a lot of overhead and fiddling with the code already in place. The idea of writing an interface has also come up, but creating a whole structure of interfaces for the sake of testing hasn't flown in management.

从我一直在阅读的内容中,我可以创建一个有问题的类的公共模型并以这种方式进行测试,但我担心在未来的某个人会对生产代码进行更改而不是将其复制到测试代码,完全破坏测试的目的。另一个选择是更改类本身的访问级别,但这将涉及大量开销和摆弄已经存在的代码。编写接口的想法也出现了,但是为了测试而创建一个完整的接口结构并没有在管理中飞行。

Am I just missing something here? What would be the best way to make sure those underlying classes are indeed functioning correctly without changing the access to them?

我在这里错过了一些东西吗?在不改变对它们的访问权限的情况下,确保这些基础类确实正常运行的最佳方法是什么?

3 个解决方案

#1


3  

You can also make a subclass of the test-subject that is in the same namespace as the test-subject, and the subclass could expose whatever features necessary for testing.

您还可以创建与测试主题位于同一名称空间中的测试主题的子类,子类可以公开测试所需的任何功能。

Assuming you have some way of giving this subclass a "test" scope, you're home free. (You dont want this class in your regular code since it breaks encapsulation)

假设您有一些方法可以将此子类赋予“测试”范围,那么您就可以免费使用。 (您不希望在常规代码中使用此类,因为它会破坏封装)

#2


4  

I'm not sure if you refer to .NET/C# projects, but you could add the InternalsVisibleTo attribute to the AssemblyInfo.cs file, to expose your internal classes to the unit test assembly.

我不确定您是否引用.NET / C#项目,但您可以将InternalsVisibleTo属性添加到AssemblyInfo.cs文件中,以将内部类公开给单元测试程序集。

Let's say you create a unit test project called "MyApplication.Tests", add this to the "MyApplication" project AssemblyInfo.cs file (located under "Properties"):

假设您创建一个名为“MyApplication.Tests”的单元测试项目,将其添加到“MyApplication”项目AssemblyInfo.cs文件(位于“Properties”下):

[assembly: InternalsVisibleTo("MyApplication.Tests")]

#3


0  

I think that your unit tests should not require anything of the source code, so the first answer certainly works. Have you considered using Reflection? I think it gets around changing the source code; there's a good discussion of this here: CodeProject

我认为您的单元测试不应该要求任何源代码,所以第一个答案肯定有效。你考虑过使用Reflection吗?我认为它可以改变源代码;这里有一个很好的讨论:CodeProject

#1


3  

You can also make a subclass of the test-subject that is in the same namespace as the test-subject, and the subclass could expose whatever features necessary for testing.

您还可以创建与测试主题位于同一名称空间中的测试主题的子类,子类可以公开测试所需的任何功能。

Assuming you have some way of giving this subclass a "test" scope, you're home free. (You dont want this class in your regular code since it breaks encapsulation)

假设您有一些方法可以将此子类赋予“测试”范围,那么您就可以免费使用。 (您不希望在常规代码中使用此类,因为它会破坏封装)

#2


4  

I'm not sure if you refer to .NET/C# projects, but you could add the InternalsVisibleTo attribute to the AssemblyInfo.cs file, to expose your internal classes to the unit test assembly.

我不确定您是否引用.NET / C#项目,但您可以将InternalsVisibleTo属性添加到AssemblyInfo.cs文件中,以将内部类公开给单元测试程序集。

Let's say you create a unit test project called "MyApplication.Tests", add this to the "MyApplication" project AssemblyInfo.cs file (located under "Properties"):

假设您创建一个名为“MyApplication.Tests”的单元测试项目,将其添加到“MyApplication”项目AssemblyInfo.cs文件(位于“Properties”下):

[assembly: InternalsVisibleTo("MyApplication.Tests")]

#3


0  

I think that your unit tests should not require anything of the source code, so the first answer certainly works. Have you considered using Reflection? I think it gets around changing the source code; there's a good discussion of this here: CodeProject

我认为您的单元测试不应该要求任何源代码,所以第一个答案肯定有效。你考虑过使用Reflection吗?我认为它可以改变源代码;这里有一个很好的讨论:CodeProject