English 中文(简体)
• 如何用单体测试的储蓄文档储存光盘?
原标题:How do I unit-test saving file to the disk?

I know that it s strongly recommended to run unit-tests in separation from file system, because if you do touch file system in your test, you also test file system itself. OK, that s reasonable.
My question is, if I want to test file saving to the disk, what do I do? As with database, I separate an interface that is responsible for database access, and then create another implementation of this for my tests? Or may be there s some other way?

最佳回答

我对这种做法的态度在(GOOS)》书指导着面向目标的软件的发展,但这符合我今天所知的最佳情况。 具体地说:

  • Create an interface to abstract away the file system from your code. Mock it where this class is needed as a collaborator/dependency. This keeps your unit-tests quick and feedback fast.
  • Create integration tests that test the actual implementation of the interface. i.e. verify that calling Save() actually persists a file to disk and has the write contents (use a reference file or parse it for a few things that it should contain)
  • Create an acceptance test that tests the whole system - end to end. Here you may just verify that a file is created - the intent of this test is to confirm if the real implementation is wired / plugged in correctly.

评论员最新情况:

如果您重读结构化的数据(如书目) (如果不能用数字表示)

interface BookRepository
{
  IEnumerable<Books> LoadFrom(string filePath);
  void SaveTo(string filePath, IEnumerable<Books> books);
}

现在,你可以使用建筑注射器向客户阶层注入一ck。 客户类别unit检测 因此,没有击中文档系统。 他们只是核实,正确的方法取决于受扶养人(如Load/Save)。

var testSubject = new Client(new Mock<BookRepository>.Object);

Next you need to create the real implementation of BookRepository that works off a File (or a Sql DB tommorrow if you want it). No one else has to know. Write integration tests for FileBasedBookRepository (that implements the above Role) and test that calling Load with a reference file gives the right objects and calling Save with a known list, persists them to the disk. i.e. uses real files These tests would be slow so mark them up with a tag or move it to a separate suite.

[TestFixture]
[Category("Integration - Slow")]
public class FileBasedBookRepository 
{
  [Test]
  public void CanLoadBooksFromFileOnDisk() {...}
  [Test]
  public void CanWriteBooksToFileOnDisk() {...}
}

最后,应当有一个/更多的<>接受测试,该试验可操作Load和 Save。

问题回答

有一种一般性规则是,对提交I/O的单位测试进行谨慎,因为它们往往过于缓慢。 但是,在单位测试中没有绝对禁止I/O档案。

在你的单位测试中,设立了一个临时目录,并细分,并在该临时名录内建立测试档案和目录。 是的,你的测验将比单纯的万国邮联测验慢,但速度仍然快。 日美特甚至有支持守则,帮助处理这一设想:@ Rule,载于 临时代用

尽管如此,大多数文件撰写法采取这种形式:

  1. Open an output stream to the file. This code must cope with missing files or file permissions problems. You will want to test that it opens the file and copes with those failure conditions.
  2. Write to the output stream. This must write in the correct format, which is the most complicated part requiring the most testing. It must cope with an I/O error while writing to the output stream, although those errors are rare in practice.
  3. Close the output stream. This must cope with an I/O error while closing the stream, although those errors are rare in practice.

只有第一个真正涉及档案系统。 其余只是使用一个产出流。

因此,你可以将中间和最后一部分从自己的方法(功能)中抽取,这种方法操纵特定产出流,而不是记名档案。 然后,你可以模拟输出流,对这种方法进行单位测试。 这些单位试验将非常快。 大多数方案拟订语言已经提供了合适的产出流类别。

这只剩下第一部分进行单位测试。 你们只需要几次测试,因此,你的试样仍然可以很快接受。

你可以不把档案名称传给你的储蓄职能,通过精简、文本查询或类似方式。 然后,如果检测你能够通过基于记忆的执行,并核实正确用具,则写上字时实际上不写任何磁盘。

为了测试问题和例外,你可以研究一个模拟框架。 这可以帮助你在储蓄过程的某一点提出具体例外,并检验贵法典如何适当处理。





相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签