为单元测试创​​建文件 [英] Creating File Just for the Sake on Unit Test

查看:24
本文介绍了为单元测试创​​建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个有趣的问题.我需要测试我是否可以成功上传和获取 PDF 文件.这适用于基于文本的文件,但我只想检查 PDF.为了运行这个单元测试,我需要一个 PDF 文件.有几个选项.我可以创建一个虚拟的 PDF 文件并将其存储在某个文件夹中,然后读取该文件并将该文件保存到系统中.但是现在,我的单元测试依赖于 PDF 文件.因此,任何运行单元测试的人都必须拥有有点糟糕的 PDF 文件.

This might be an interesting question. I need to test that if I can successfully upload and fetch the PDF file. This works for the text based files but I just wanted to check for PDF. For this unit test to run I need a PDF file. There are couple of options. I can create a dummy PDF file and store it some folder and read that file and save the file to the system. But now, my unit test is dependent on the PDF file. So, anyone who runs the unit test must have the PDF file which is kinda bad.

我的另一种方法是创建 PDF 文件.这没什么大不了的,因为我可以简单地创建一个扩展名为 .pdf 的虚拟文件,或者我什至可以使用一些 PDF 第三方工具来创建 PDF 文件.

Another way for me is to create a PDF file. This is not a big deal as I can simply create a dummy file with the .pdf extension OR I can even use some PDF third party tool to create PDF file.

另一种方法是将 PDF 文档作为嵌入式资源嵌入,然后从程序集中提取.

Another way also is to embed the PDF document as an embedded resource and then extract that from the assembly.

您认为处理此问题的最佳方法是什么?

What do you think is the best way to handle this issue?

推荐答案

在资源目录中保存一个包含测试的 PDF 文件.您的测试应该尽可能简单,创建文件只是一个可能失败的地方.

Save a PDF file with your tests in a resources directory. Your tests should be as simple as possible, and creating a file is just one more point that could fail.

这篇关于为单元测试创​​建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆