如何做集成测试的.NET与真正的文件?如何做、文件、测试、NET

2023-09-03 09:09:27 作者:花开花败旧人不在

我有一些类,实现了一些逻辑文件系统和文件有关。例如,我在执行以下任务,因为这逻辑的一部分: - 检查如果某些文件夹具有一定的结构(例如,它包含子文件夹的具体名称等...) - 从这些文件夹加载一些文件,并检查其结构(例如,这些都是位于某个文件夹中的某些地方某些配置文件。) - 负荷配置文件中的测试/验证的其他文件(如:该配置文件包含有关在同一文件夹中的其他文件,应该还有其他的内部结构信息等)

I have some class that implements some logic related to file system and files. For example, I am performing following tasks as part of this logic: - checking if certain folder has certain structure (eg. it contains subfolders with specific names etc...) - loading some files from those folders and checking their structure (eg. these are some configuration files located at certain place within certain folder) - load additional files for testing/validation from the configuration file (eg. this config file contains information about other files in the same folder that should have other internal structure etc...)

现在这一切的逻辑有一定的工作流程,并抛出异常,如果事情是不正确的(如:配置文件没有找到在特定的文件夹位置)。此外,还有托管扩展性框架(MEF)参与在这样的逻辑,因为其中的某些文件我检查被管理的DLL,我手动加载到MEF骨料等...

Now all this logic has some workflow and exceptions are thrown if something is not right (eg. configuration file is not found at the specific folder location). In addition, there is Managed Extensibility Framework (MEF) involved in this logic because some of these files I am checking are managed DLLs that I am manually loading to MEF aggregates etc...

现在我想测试这一切都以某种方式。我在想创造的硬盘,涵盖各种测试用例然后对他们我的code几个物理测试文件夹。我可以创建例如: - 文件夹正确的结构和所有的文件是有效的 - 文件夹正确的结构,但无效的配置文件 - 文件夹正确的结构,但缺少配置文件 等等...

Now I'd like to test all this in some way. I was thinking of creating several physical test folders on HDD that cover various test cases and then run my code against them. I could create for example: - folder with correct structure and all files being valid - folder with correct structure but with invalid configuration file - folder with correct structure but missing configuration file etc...

请问这是正确的做法?我不知道,虽然究竟是如何在这种情况下运行我的code ......我当然不希望运行的整个应用程序并将其指向检查这些嘲笑文件夹。我应该使用一些单元测试框架一种编写单元测试,对这些文件系统对象执行我的code?

Would this be the right approach? I am not sure though how exactly to run my code in this scenario... I certainly don't want to run the whole application and point it to check these mocked folders. Should I use some unit testing framework to write kind of "unit tests" that executes my code against these file system objects?

在一般情况下,是这一切正确的做法对于这种测试场景?是否有其他更好的方法?

In general, is all this a correct approach for this kind of testing scenarios? Are there other better approaches?

推荐答案

首先,,我认为,这是更好地编写单元测试时不接触任何外部资源来测试你的逻辑。在这里,你有两个选择:

First of all, I think, it is better to write unit tests to test your logic without touching any external resources. Here you have two options:

您需要使用抽象层,从外部依赖,例如文件系统隔离开你的逻辑。您可以轻松地存根或模拟(用手或用有限的隔离框架,如NSubstitute,FakeItEasy或起订量的帮助)这个抽象的单元测试。我preFER这个选项,因为在这种情况下,测试督促他们更好的设计。 如果你有一个处理遗留code(也只有在这种情况下),你可以使用无约束的隔离框架之一(如TypeMock隔离,JustMock或Microsoft正版正货),可以存根/模拟pretty的每样东西(例如,密封,静态类,非虚拟方法)。但是,他们要花钱的。只有免费选项是微软正版正货,除非你的Visual Studio 2012/2013 premium /旗舰版。的快乐用户

在单元测试中你不需要测试外部库的逻辑,如MEF。

In unit tests you don't need to test logic of external libraries such as MEF.

第二,如果你想要写的集成测试,然后你需要写幸福路测试(如果一切正常)和一些测试,测试你的逻辑在边界情况(文件或目录未找到)。不同于@Sergey Berezovskiy,我建议为每个测试用例 单独的文件夹。的主要优点是:

Secondly, if you want to write integration tests, then you need to write "happy path" test (when everything is OK) and some tests that testing your logic in boundary cases (file or directory not found). Unlike @Sergey Berezovskiy, I recommend to create separate folders for each test case. The main advantages is:

您可以给您的文件夹有意义的名字更清晰的前preSS您 意图; 您不需要编写复杂的(即脆弱的)安装/拆卸的逻辑。 即使以后决定使用另一种文件夹结构,那么你就可以更容易地改变它,因为你已经工作code和测试(在测试工具重构是容易得多)。 you can give your folder meaningful names that more clearly express your intentions; you don't need to write complex (i.e. fragile) setup/teardown logic. even if you decide later to use another folder structure, then you can change it more easily, because you will already have working code and tests (refactoring under test harness is much easier).

对于这两个,单元测试和集成测试,您可以使用普通的单元测试框架(如NUnit的或xUnit.NET)。有了这个框架是pretty的容易发动您在持续集成场景测试您的生成服务器上。

For both, unit and integration tests, you can use ordinary unit testing frameworks (like NUnit or xUnit.NET). With this frameworks is pretty easy to launch your tests in Continuous integration scenarios on your Build server.

如果您决定写两种类型的测试,然后点击您需要从集成测试独立的单元测试(您可以创建各种测试单独的项目)。理由是:

If you decide to write both kinds of tests, then you need to separate unit tests from integration tests (you can create separate projects for every kind of tests). Reasons for it:

单元测试是一个安全网,为开发者。他们必须提供关于系统各单位后,最后code的变化(bug修​​正,新功能)预期的行为快速反馈。如果他们频繁地运行,那么开发人员可以快速,轻松地找出一张code,打破了系统。没有人愿意运行缓慢单元测试。 集成测试通常比单元测试要慢。但是他们有不同的用途。他们会检查预期与现实的依赖单位工程。 unit tests is a safety net for developers. They must provide quick feedback about expected behaviour of system units after last code changes (bugfixes, new features). If they are run frequently, then developer can quickly and easily identify piece of code, that broke the system. Nobody wants to run slow unit tests. integration tests are generally slower than unit tests. But they have different purpose. They checks that unit works as expected with real dependencies.