JUnit:可以(应该)这样做吗? [英] JUnit: Can (should) this be done?

查看:122
本文介绍了JUnit:可以(应该)这样做吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为网络应用编写一些UI测试,但有一些复杂功能,我希望你能帮我解决。

I'm trying to write some UI-Tests for a web app, but there are a couple of complications that I hope you could help me resolve.

首先,该应用程序有两种模式。其中一种模式是训练,另一种模式是现场。在实时模式下,数据直接从我们的数据库中获取,我的测试所做的任何更改都反映在数据库中,然后在实时运行每个测试之前我需要生成测试数据,因为它每次都不同(我已经有了这个逻辑)到位)。在训练模式下,所有数据都是静态的,因此每次都使用相同的测试数据。

First of all the app has two modes. One of the modes is 'training' and another is 'live'. In live mode the data is taken straight from our database and any changes made by my tests are reflected in the database, before running each test in 'live' I need to generate the test data as it's different every time (I already have that logic in place). In training mode all of the data is static so the same test data is used every time.

由于每个模式的UI都相同,我想运行测试一次对于每种模式,但由于某些特殊性,我的一些测试只能在实时模式下运行。

As the UI is the same in each mode I want to run the tests once against each mode, however due to certain peculiarities some of my tests can only be ran in live mode.

我最喜欢这样做是为了分裂我的测试几个类,比如:
UserTests,
PaymentTests,
LiveOnlyTests,
等。并且有两个不同的类(扩展通用接口)提供测试数据。

How I'd like to do this ideally is to have split my Tests into a couple of classes, say: UserTests, PaymentTests, LiveOnlyTests, etc. And have two different classes (extending a common interface) that provide the test data.

然后我想建立两个不同的测试套件,每个模式一个。测试套件会接受带有我传递数据的对象并执行测试。

Then out of those pieces I'd like to build 2 different test suites, one for each mode. The test suites would accept the object with data that I pass them and execute the tests.

有没有人知道这个或类似的效果是否可以做到JUnit(或任何其他java测试框架)。我可以看到这个完成的唯一方法是编写我自己的测试运行器,但我宁愿避免这种情况。

Does anyone know if this or something that achieves a similar effect is possible to do in JUnit (or any other java-test framework for that matter). The only way I can see this getting done is through writing my own test-runner, but I'd rather avoid that.

提前致谢。

推荐答案

我要做的是查看参数化假设

参数化允许您为测试定义一组测试数据。您可以返回training / live的数据集(可能基于System属性)。

Parameterized allows you to define a set of test data for a test. You could return the data set for the training/live (based perhaps on a System property).

假设允许您测试某些内容,如果假设为false,则为中止测试(假设失败,而不是失败)。

Assume allows you to test something, and if the assumption is false, it aborts the test (with a failed assumption, NOT a failure).

你也可以使用类别,但使用Parameterized运行它们更难,因为必须组合两个跑步者。

You could also use Categories, but running them with Parameterized is harder, because have to combine two runners.

因此,假设参数化&假设:

So, assuming parameterized & assume:

@RunWith(Parameterized.class)
 public class MyTest {
  @Parameters
  public static List<Object[]> data() {
    // return different data, depending upon the value of System.getProperty("testType");
    return Arrays.asList(new Object[][] {
      { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }
    });
  }

  private int fInput;
  private int fExpected;

  public MyTest(int input, int expected) {
    fInput= input;
    fExpected= expected;
  }

  @Test
  public void testTraining() {
    Assume("training".equals(System.getProperty("testType"));

    // test which only runs for training, using the data from data()
  }

  @Test
  public void testBoth() {
    // test which only runs for both, using the data from data()
  }
 }

通过上述的一些组合,你可以得到你想要的东西。

With some combination of the above, you can probably get what you want.

这篇关于JUnit:可以(应该)这样做吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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