junit进行单元测试_通过JUnit规则轻松进行AppEngine单元测试

junit进行单元测试

by Ramesh Lingappa

通过拉梅什·林加帕(Ramesh Lingappa)

通过JUnit规则轻松进行AppEngine单元测试 (AppEngine unit testing made easy with JUnit Rules)

Hello there! If you are using AppEngine for hosting your application, then you will be using one or more of their services like Datastore, Memcache, TaskQueues, UserService, etc.

你好! 如果您使用AppEngine托管应用程序,则将使用它们的一项或多项服务,例如数据存储,Memcache,TaskQueues,UserService等。

And you will be needing to write unit tests to make sure your application functionality is working as expected with these services. For that, you need to set up some configurations to test these services in your local environment.

而且,您将需要编写单元测试,以确保您的应用程序功能可以按预期使用这些服务。 为此,您需要设置一些配置以在本地环境中测试这些服务。

If you are new to AppEngine service testing, Google has great documentation on how to set up this configuration with sample codes. Take a look at Local Unit Testing for Java.

如果您不熟悉AppEngine服务测试,Google会提供有关如何使用示例代码设置此配置的出色文档。 看一下Java的本地单元测试

For example, here is a sample code from Google to perform datastore testing:

例如,以下是Google进行数据存储区测试的示例代码:

This is great. Most of the time, our services will be making use of multiple AppEngine services like Memcache for entities (Objectify), queueing tasks after saving an entity. The real pain point comes when we try to set up multiple configurations.

这很棒。 大多数时候,我们的服务将使用多个AppEngine服务,例如用于实体(Objectify)的Memcache,在保存实体后对任务进行排队。 当我们尝试设置多种配置时,真正的痛点就来了。

Okay, that’s the lot of configuration, but guess what — it’s not all. What if you need to change specific settings for some test like High Replication, Queue XML Path, or Current User ? Then your set up will be even more complex. And you need to repeat all these for each of your unit test classes.

好的,这就是很多配置,但是请猜测-并非全部。 如果您需要为某些测试(例如高复制队列XML路径当前用户)更改特定设置怎么办? 这样您的设置将更加复杂。 您需要为每个单元测试类重复所有这些操作。

Oh, by the way, did you forgot Objectify test setup? Take a look at Objectify Unit Testing.

哦,对了,您忘记了Objectify测试设置吗? 看一下Objectify单元测试

You might be thinking,

你可能在想

“Let’s set up all of these configurations in the parent class and every unit testing class will extend this one”
“让我们在父类中设置所有这些配置,每个单元测试类都将对此进行扩展”

We can do that, but there is another problem with it:

我们可以做到,但是还有另一个问题:

Each of these services is expensive to create. You need to configure the environment using only the services you need.

这些服务中的每一个创建起来都很昂贵。 您只需要使用所需的服务来配置环境

So unnecessarily configuring all services will probably slow down the test execution time.

因此,不必要地配置所有服务可能会减慢测试执行时间。

好吧,有救助者吗? (Okay, is there a rescuer?)

Yes, Junit Rules comes to the rescue!

是的, Junit Rules助您一臂之力

“Rules allow very flexible addition or redefinition of the behavior of each test method in a test class. Testers can reuse or extend one of the provided Rules below, or write their own”
“规则允许非常灵活地添加或重新定义测试类中每种测试方法的行为。 测试人员可以重复使用或扩展以下提供的规则之一,也可以编写自己的规则”

There are so many great articles out there about Junit Rules — please check them out.

关于Junit规则的文章太多了-请检查一下。

So with JUnit rules, we are able to setup external dependencies before test execution in an easy way. With a few tweaks, we can setup AppEngine related services per class in the way we wanted. Here is a sample:

因此,借助JUnit规则,我们能够以简单的方式在测试执行之前设置外部依赖项。 通过一些调整,我们可以按照需要的方式为每个类设置与AppEngine相关的服务。 这是一个示例:

Here AppEngineRule is a simple class that extends ExternalResource and was created using a builder pattern. We can set up services which we need, and if you see in the SampleTestClass, we can do all the setup in one line.

这里AppEngineRule是一个简单的类,它扩展了ExternalResource并使用构建器模式创建。 我们可以设置所需的服务,如果您在SampleTestClass中看到,我们可以在一行中完成所有设置。

@Rule    public final AppEngineRule rule = AppEngineRule.builder()             .withDatastore()             .withQueue()             .build();

And AppEngineRule class overrides before and after methods to setup AppEngine setup and tearDown functionalities. You can also configure similarly for each Test classes with only the required services.

而且AppEngineRule类会覆盖设置AppEngine设置和tearDown功能的方法beforeafter 。 您还可以仅使用所需的服务为每个测试类别进行类似配置。

这就是全部? 我们可以做得更好吗? (Is that all? Can we do any better?)

Of course we can! To make this setup easy you need to write something similar to AppEngineRule class with all the boilerplate setup code.

当然,我们可以! 为了简化此设置,您需要使用所有样板设置代码编写类似于AppEngineRule类的内容。

Here is good news: you don’t need to write any. I made a small library with all the necessary setup implementation for all the services and with even more configurable options.

这是个好消息:您无需编写任何内容。 我制作了一个小型库,其中包含所有服务的所有必要设置实现以及甚至更多可配置的选项。

ramesh-dev/gae-test-util-javaGoogle AppEngine Java Utility Library. Contribute to ramesh-dev/gae-test-util-java development by creating an account…github.com

ramesh-dev / gae-test-util-java Google AppEngine Java实用程序库。 通过创建帐户来为ramesh-dev / gae-test-util-java开发做出贡献…… github.com

In your build script, simply add the dependency, for example in gradle:

在构建脚本中,只需添加依赖项,例如在gradle中:

testCompile 'com.github.ramesh-dev:gae-test-util-java:0.3'

With this, we can have flexible configurations like the following:

这样,我们就可以进行如下灵活的配置:

结论 (Conclusion)

So Junit Rules is a handy option to easily configure our external dependencies, even for complex one like AppEngine services. You can also have other Rules and chain them in a specific order using Rule Chain

因此,即使对于像AppEngine服务这样的复杂项目,Junit Rules是一个方便的选项,可以轻松地配置我们的外部依赖项。 您还可以拥有其他规则,并使用“ 规则链”以特定顺序链接它们

Thank you for reading and this time, and Happy Testing…

感谢您的阅读和这次,以及“测试愉快”…

参考文献 (References)

翻译自: https://www.freecodecamp.org/news/appengine-unit-testing-made-easy-with-junit-rules-97c2127a161a/

junit进行单元测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值