[英]Skip unit test if csv file not found
I have a number of unit tests which rely on the presence of a csv file. 我有许多依赖于csv文件存在的单元测试。 They will throw an exception if this file doesn't exist obviously.
如果此文件显然不存在,它们将抛出异常。
Are there any Gallio/MbUnit methods which can conditionally skip a test from running? 是否有任何Gallio / MbUnit方法可以有条件地跳过测试运行? I'm running Gallio 3.1 and using the CsvData attribute
我正在运行Gallio 3.1并使用CsvData属性
[Test]
[Timeout(1800)]
[CsvData(FilePath = TestDataFolderPath + "TestData.csv", HasHeader = true)]
public static void CalculateShortfallSingleLifeTest()
{
.
.
.
Thanks 谢谢
According to the answer in this question , you'll need to make a new TestDecoratorAttribute
that calls Assert.Inconclusive
if the file is missing. 根据这个问题的答案,如果文件丢失,你需要创建一个调用
Assert.Inconclusive
的新TestDecoratorAttribute
。
Assert.Inconclusive
is very appropriate for your situation because you aren't saying that the test passed or failed; Assert.Inconclusive
非常适合您的情况,因为您并不是说测试通过或失败; you're just saying that it couldn't be executed in the current state. 你只是说它无法在当前状态下执行。
What you have here is not a unit test. 你在这里所拥有的不是单元测试。 A unit test tests a single unit of code (it may be large though), and does not depend on external environmental factors, like files or network connections.
单元测试测试单个代码单元(虽然可能很大),并且不依赖于外部环境因素,如文件或网络连接。
Since you are depending on a file here, what you have is an integration test. 由于您依赖于此处的文件,因此您所拥有的是集成测试。 You're testing whether your code safely integrates with something outside of the control of the code, in this case, the file system.
您正在测试您的代码是否安全地与代码控制之外的东西集成,在本例中是文件系统。
If this is indeed an integration test, you should change the test so that you're testing the thing that you actually want tested. 如果这确实是一个集成测试,那么您应该更改测试,以便测试您实际需要测试的内容。
If you're still considering this as a unit test, for instance you're attempting to test CSV parsing, then I would refactor the code so that you can mock/stub/fake out the actual reading of the CSV file contents. 如果您仍然将此视为单元测试,例如您正在尝试测试CSV解析,那么我将重构代码,以便您可以模拟/存根/伪造CSV文件内容的实际读取。 This way you can more easily provide test data to the CSV parser, and not depend on any external files.
这样,您可以更轻松地向CSV解析器提供测试数据,而不依赖于任何外部文件。
For instance, have you considered that: 例如,您是否考虑过:
Once you start involving external systems like file systems, network connections, etc. there's so many things that can go wrong that what you have is basically a brittle test. 一旦你开始涉及外部系统,如文件系统,网络连接等,有很多事情可能会出错,你所拥有的基本上是一个脆弱的测试。
My advice: Figure out what you're trying to test (file system? CSV parser?), and remove dependencies that are conflicting with that goal. 我的建议:找出你要测试的内容(文件系统?CSV解析器?),并删除与该目标冲突的依赖项。
An easy way would be to include an if condition right at the start of the test that would just execute any code in the test if the CSV file can be found. 一种简单的方法是在测试开始时包含if条件,如果可以找到CSV文件,则只执行测试中的任何代码。
Of course this has the big drawback that tests would be green although they haven't actually run and asserted anything. 当然,这有一个很大的缺点,即测试是绿色的,尽管它们实际上并没有运行和断言。
I agree with Grzenio though, if you have unit tests that rely heavily on external conditions, they're not really helping you. 我同意Grzenio,如果你有严重依赖外部条件的单元测试,他们并没有真正帮助你。 In this scenario you will never really know whether the unit test ran successfully or was just skipped, which contradicts what unit tests are actually for.
在这种情况下,您永远不会真正知道单元测试是成功运行还是刚跳过,这与单元测试的实际情况相矛盾。
In my personal opinion, I would just write the test so that they correctly fail when the file is not there. 在我个人看来,我只是编写测试,以便在文件不存在时它们正确失败。 If they fail this is an indicator that the file in question should be available on the machine where the unit tests run.
如果它们失败,则表明有问题的文件应该在运行单元测试的机器上可用。 This might need some manual adjustments at times (getting the file to the computer or server in question), but at least you have reliable unit tests.
这可能需要一些手动调整(将文件提供给相关的计算机或服务器),但至少你有可靠的单元测试。
In Gallio/MbUnit v3.2 the abstract ContentAttribute
and its concrete derived types (such as [CsvData]
have a new optional parameter that allows to change the default outcome of a test in case of an error occured while opening or reading the file data source (ref. issue 681 ). The syntax is the following: 在Gallio / MbUnit v3.2中,抽象
ContentAttribute
及其具体派生类型(如[CsvData]
有一个新的可选参数,允许在打开或读取文件数据源时发生错误时更改测试的默认结果(参见问题681 )。语法如下:
[Test]
[CsvData(..., OutcomeOnFileError = OutcomeOnFileError.Inconclusive)]
public void MyTestMethod()
{
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.