[英]what is the best way to calculate the expected_value in assertEquals() method in jUnit
I'm using the assertEquals()
method in jUnit
to test a certain value is equals or not to the actual value the code generates. 我在
jUnit
使用assertEquals()
方法来测试某个值是否等于代码生成的实际值。
/*
calculating the actual_value
*/
int expected_value = 1000; // rows of the set of files, manually calculated
assertEquals(expected_value, actual_value);
I'm wondering if I do it something like below will that be a problem, in case of standards and formalities. 在标准和手续的情况下,我想知道如果我这样做,那将是一个问题。
/*
calculating the actual_value
*/
int expected_value = getRelevantLinesOfFiles(set of files); // rows of the set of files
assertEquals(expected_value, actual_value);
since it's almost impossible to always find that kind of variable manually, I've written a method to read and calculate the relevant lines in those files. 因为几乎不可能总是手动找到那种变量,所以我写了一个方法来读取和计算这些文件中的相关行。
My concern is that I'm using an out put of a method in assertEquals
testing. 我担心的是我在
assertEquals
测试中使用了一个方法。 But the getRelevantLinesOfFiles()
method is not tested. 但是没有测试
getRelevantLinesOfFiles()
方法。 If I'm going to test it, then again i have to manually read the files. 如果我要测试它,那么我必须再次手动读取文件。 So it's kinda same thing again and again.
所以它一次又一次有点相同。
Is that a good practice ? 这是一个好习惯吗? or what is the best way to do these kind of testing ?
或者进行这类测试的最佳方法是什么?
If those files are also the input that actual_value
is calculated from, what you're doing is testing an alternative implementation vs the real one. 如果这些文件也是计算
actual_value
的输入,那么你正在做的是测试替代实现与真实实现。 That's valid but requires understanding things up front, eg it's usually done with a very simple and easy-to-review test implementation compared with an optimized and more complicated 'production' implementation. 这是有效的,但需要预先了解事情,例如,与优化和更复杂的“生产”实施相比,通常使用非常简单且易于审查的测试实施。 If that's not the case you're doing something wrong.
如果情况并非如此,那你做错了什么。
If the files contain results that actual_value
isn't calculated from then it should be ok, eg if you have sets of inputs and matching sets of expected output. 如果文件包含不计算
actual_value
结果,则应该没问题,例如,如果您有输入集和预期输出的匹配集。
Also, consider whether you can distill at least a few cases of trivial hard-coded input and hard-coded expected output, similar to your first example, not involving files. 此外,考虑是否可以提取至少一些简单的硬编码输入和硬编码预期输出的情况,类似于您的第一个示例,不涉及文件。 This may require allowing your interface to work with an abstraction that isn't a
File
in order to mock input, or to be able to inject an alternative file-reading mechanism in tests that actually serves mock test data. 这可能需要允许您的接口使用非
File
的抽象来模拟输入,或者能够在实际服务模拟测试数据的测试中注入替代文件读取机制。
EDIT: just to provide a concrete example of a great abstraction to use instead of File
instances (or filenames, or whatever) consider using okio and passing in a set of Source instances. 编辑:只是提供一个很好的抽象的具体示例,而不是使用
File
实例(或文件名,或其他)考虑使用okio并传入一组Source实例。
Real implementation : given a list of File
instances, create Source
instances using Okio.source(file) . 真正的实现 :给定一个
File
实例列表,使用Okio.source(文件)创建Source
实例。
Tests : pass a list of Buffer instances containing whatever you want 测试 :传递包含您想要的任何内容的Buffer实例列表
Buffer b = new Buffer();
b.writeUtf8("whatever the hell I want in this file");
// can also write bytes or anything else
int actualValue = getRelevantLinesOfFiles(Arrays.asList(b));
if the test files are generated only for test, i think you should carefully prepare these test files manually, for example, 'file1' has 1 line, file0
has 0 line, and 'fileX' has X lines, etc. no need prepare too much files, you can just consider some critical cases. 如果只为测试生成测试文件,我认为你应该手动准备这些测试文件,例如,'file1'有1行,
file0
有0行,'fileX'有X行等,不需要准备很多文件,你可以考虑一些关键的案例。
if these files are real data from production environment, then i suggest you write a method, like the getRelevantLinesOfFiles
in your code, to count the line number of them. 如果这些文件是来自生产环境的真实数据,那么我建议你编写一个方法,比如代码中的
getRelevantLinesOfFiles
,来计算它们的行号。 but firstly, you should test this method using the approach i mentioned above. 但首先,您应该使用我上面提到的方法测试此方法。
It is always good practice to leave "Magic Numbers" (aka 1000) out of your code. 将“Magic Numbers”(又名1000)留出代码总是好的做法。 Like talex said, test the
getRelevantLivesOfFiles()
method on files small enough to count. 就像talex所说的那样,测试
getRelevantLivesOfFiles()
方法对文件小到可以计算。 Then you can use it with confidence to test the other parts of your code. 然后,您可以放心地使用它来测试代码的其他部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.