Android 中的 JUnit

发布于 2024-09-30 13:17:35 字数 192 浏览 0 评论 0原文

我熟悉 android 中的 JUnit 测试.. 我的问题是,如果我们使用计算器并且我们想要测试加法运算。如果我们使用更多数量的测试用例(例如 30)来测试加法运算。有没有通用的方法可以做到这一点,或者有没有办法从 Excel 表或 xml 文件中获取测试用例,而不是重写测试用例 30 次?

请让我知道是否有更好的方法...

提前致谢

I am familiar with JUnit testing in android..
My Question is if we are using calculator and we want to test addition operation..To test the addition operation if we are using more number of test cases(for example 30). instead of rewriting the test cases for 30 times, is there any generic way to do this or is there any way to take the test cases form excel sheet or xml file..?

Please let me know is there any better way ...

Thanks in advace

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戈亓 2024-10-07 13:17:35

您可以通过创建一个函数并在每个 JUnit 测试中调用它来减少重复代码。例如:

public void test_add_zeroPlusZero()
{
  Assert.assertTrue("Failed on 0+0.", testAdd(new int[] {0,0}),0);
}

private boolean testAdd(int[] values, int expectedValue)
{
  // Try to add.
  // If adding fails, return false.
  // If adding succeeds, return true.
}

对于读取大量测试值,您不能只从文件中读取多行整数(代表要添加的值和预期结果,每个值用空格或其他内容分隔)然后将它们放入上面显示的 testAdd 函数(或等效函数)?在伪代码中,这可能看起来像:

public void test_add_from_file()
  File testValueFile = get file with test values()
  while ((nextLine = testValueFile.readLine) != END_OF_FILE)
    int[] values = parse values from nextLine
    int expectedValue = parse expected value from nextLine
    Assert.assertTrue("Couldn't add the following values: " + values, testAdd(values, expectedValue))

You could reduce duplicate code by making a function and calling it in each JUnit test. E.g.:

public void test_add_zeroPlusZero()
{
  Assert.assertTrue("Failed on 0+0.", testAdd(new int[] {0,0}),0);
}

private boolean testAdd(int[] values, int expectedValue)
{
  // Try to add.
  // If adding fails, return false.
  // If adding succeeds, return true.
}

As for reading in huge numbers of test values, couldn't you just read in multiple rows of integers (representing the values to add and the expected result, each value separated by a space or something) from file and then put them through the testAdd function (or equivalent) shown above? In pseudocode, this might look like:

public void test_add_from_file()
  File testValueFile = get file with test values()
  while ((nextLine = testValueFile.readLine) != END_OF_FILE)
    int[] values = parse values from nextLine
    int expectedValue = parse expected value from nextLine
    Assert.assertTrue("Couldn't add the following values: " + values, testAdd(values, expectedValue))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文