从 IDE 中在远程服务器上运行 TestNG/JUnit 集成测试

发布于 2024-11-14 12:52:03 字数 515 浏览 2 评论 0原文

在我的 IDE(Eclipse 或 NetBeans,没关系)中,我有一些 TestNG 测试类(但我的问题也指远程 JUnit 测试),它们是集成测试。这些测试需要集成服务器才能运行,它们不能在本地计算机上运行。他们需要集成服务器的完整环境 - 不仅仅是 JavaEE 容器相关的东西(=> 没有 Arquillian 也没有 JEEUnit)。

现在我希望能够从我的 IDE (Eclipse) 中运行这些测试 - 最好使用 TestNG 插件 - 但当我启动它们时,它们实际上应该在远程集成服务器上运行。

是否可以从我的 IDE 中在远程服务器上启动集成测试? 我喜欢在远程服务器上安装某种代理来等待测试请求并执行它们的想法。但正如我所说,如果它从 TestNG 插件内部运行,那就太好了。

我是否需要某种解决方法,例如 Ant 脚本(希望不需要)或一些 Maven 魔法?最佳实践是什么?

我知道我还可以为我的应用程序创建 Web 服务,然后我可以从本地单元测试中调用它们。但我想知道没有 Web 服务是否也有可能。

Inside my IDE (Eclipse or NetBeans, doesn't matter), i have some TestNG testclasses (But my question also refers to remote JUnit tests), which are Integration tests. These tests need an integration server to run, they cannot be run on a local machine. They need the complete environment of the integration server - not only JavaEE container related stuff (=> no Arquillian nor JEEUnit).

Now I want to be able to run these tests from within my IDE (Eclipse) - preferrably with the TestNG Plugin - but when I launch them they should actually be run on the remote integration server.

Is it possible to launch integration tests on a remote server from within my IDE?
I like the idea of having some kind of Agent on the remote Server which waits for test-requests and executes them. But as I said, it would be nice if this runs from inside the TestNG Plugin.

Do I need some kind of workaround, for example Ant scripts (hopefully not) or some Maven magic? What are the best practices?

I know I could also create Webservices for my application, then I can call them from local unit tests. But I'd like to know if there are also possibilities without Webservices.

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

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

发布评论

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

评论(4

毁梦 2024-11-21 12:52:03

如何完成此任务在很大程度上取决于您使用的集成服务器和 IDE,以及您的应用程序是什么。我假设您使用的是 Eclipse;我假设你正在使用 Jenkins,因为在我看来,它是最容易让它执行任务的你想要的。

其中大部分都可以开箱即用。然而,有一点需要一些额外的工作。

您需要执行以下操作:

  1. 在 Jenkins 中设置一个名为“集成测试”的作业(名称并不重要)。将其配置为运行您希望能够从 IDE 运行的测试,并允许远程计算机触发构建。
  2. 使该作业成为参数化作业,并添加文件参数。让我们假设这称为“测试程序”。这将是作业将测试的程序。
  3. 使集成测试作业使用TestingProgram 文件来运行测试。如果您的程序需要多个文件,则假设这将是一个包含所有必需文件的 zip 文件。
  4. 配置您的 Eclipse 项目以生成集成测试作业所需的相同文件(我假设这已经发生,因为它可能正在构建程序的某种 bin 版本)

现在是连接的稍微复杂的部分Eclipse 到詹金斯。不幸的是,我认为没有任何现有工具可以完全满足您的需求。好消息是,使用自定义脚本可以非常简单地实现。所有脚本要做的是:

  1. 如果您的程序需要多个文件,而 eclipse 还没有这样做,则需要将这些文件压缩起来
  2. 需要获取文件并将其读入某个变量,然后进行 Base64 编码它。有很多库可以为您解决大部分问题,例如 这个。假设此文件被读入名为 $programFile 的变量中,
  3. 它将需要向 http://:8080//buildwithparameters?TestingProgram=$programFile

您可以在 jenkins 文档

完成这些步骤的脚本几乎可以是任何东西。鉴于您希望最终将其合并到您的 IDE 中,似乎最合乎逻辑的选择是 ant 脚本或 Eclipse 插件。两者都不会太复杂 - ant 脚本只会执行这些步骤,并且您可以将 ant 脚本导入到专门用于测试的 Eclipse 项目中 - 并且插件可以只添加一个菜单项,当在项目中运行时,该菜单项将执行该项目的上述步骤。

注意: 实际上有几种不同的方法可以使用 Jenkins 触发带有参数的构建。例如,您可以执行 POST 请求,使用 json 传递我链接到的 jenkins 文档中描述的参数,使用 Jenkins CLI 等。并非所有这些都可以使用文件参数,但您可以在非常相似的方式 - 作为自定义脚本中的一个步骤,您将在 Jenkins 上执行远程构建,并传递要用于测试的文件。例如,我给出的解释假设测试文件非常小;如果不是这种情况,您可能需要执行 POST 请求。如果您在使用一种方法时遇到问题,应该很容易切换到使用效果更好的其他方法。

How you will accomplish this depends a lot on what integration server and IDE you're using, and what you're application is. I'll assume you're using Eclipse; and I'll assume you're using Jenkins, since it will IMO be the easiest to get it to do what you want.

Most of this will work out of the box. However, there is a little bit that will require a some of additional work.

You'll want to do the following:

  1. Set up a job in Jenkins named something like "Integrated Testing" (the name doesn't matter). Configure it to run the tests you want to be able to run from your IDE, and to allow remote machines to trigger builds.
  2. Make this job a parameterized job, and add a file parameter. Lets suppose this is called 'TestingProgram'. This will be the program that the job will test.
  3. Make the Integrated Testing job use the TestingProgram file to run the tests. If you're program requires more than one file, than assume this will be a zip file containing all of the necessary files.
  4. Configure your Eclipse project to produce these same file(s) that the Integration Testing job expects (I assume this is already happening, since it is presumably building some sort of bin version of your program)

Now comes the slightly more complicated part of hooking up Eclipse to Jenkins. Unfortunately, I don't think there are any preexisting tools that will do exactly what you want. The good news is that it should be very simple to achieve with a custom script. All the script has to do is:

  1. If your program has multiple files required, and eclipse does not already do so, it will need to zip these files up
  2. It will need to take the file and read it into some variable, and then base64 encode it. There are lots of libraries that will take care of most of this for you, eg this one. Lets assume this file is read into a variable called $programFile
  3. It will need to send a HTTP request to http://<your-jenkins-server>:8080/<integrated-testing-job-name>/buildwithparameters?TestingProgram=$programFile

You can read up more on triggering Jenkins remote builds with parameters in the jenkins docs.

The script that accomplishes these steps can be pretty much anything. Given that you want to ultimately incorporate it into your IDE, it seems like the most logical choices would be an ant script or an Eclipse plugin. Neither would be too complicated - the ant script would just do those steps, and you could import the ant script into an Eclipse project specifically for doing testing - and a plugin could just add a menu item which, when run within a project, would execute the above steps for that project.

NOTE: There are actually several different ways to trigger a build with parameters using Jenkins. For example, you can do a POST request, use json to pass the parameter as described in the jenkins docs I linked to, use the Jenkins CLI, etc. Not all of them work with file parameters, but you would use them all in a very similar way - as a step in your custom script, you would execute a remote build on Jenkins, and pass the file you want to use to test with. For example, the explanation I gave assumes that the testing file is very small; if that's not the case, you might want to do a POST request instead. If you run into problems using one method, it should be pretty easy to switch it to use a different method that works better.

梦屿孤独相伴 2024-11-21 12:52:03

不幸的是,我没有现成的解决方案,但我想我可以给你一些提示,因为我花了一些时间思考这个问题。

我不了解 TestNG,但 JUnit 能够插入您自己的测试执行器。我确信 TestNG 具有适当的功能。所以,找到它并熟悉它。由于您可以控制测试的调用方式,因此您甚至可以执行其他操作,而不是调用测试用例的方法。例如,调用一些远程 API,使测试能够远程运行。

显然,它可以是使远程代理(根据您的建议)在远程计算机上运行测试的Web服务。很好,但我更喜欢无代理或半无代理的解决方案。我是什么意思?例如,如果您的远程计算机是 Unix,您可以执行 SSH 或 Telnet 连接并运行命令行。在这种情况下,您可以创建 mvn 或 ant 脚本,使用 ssh 复制到远程计算机,然后运行它。该脚本将运行您的测试。所以它几乎是无代理的。您只需要 java 安装和 SSH 支持。

如果远程计算机是 Windows,则可以使用 Telnet 或 WMI。因此,如果安全不是问题但您需要跨平台支持,请使用 Telnet。

如果您需要有关 SSH/Telnet 的更多帮助,请随时与我联系。

Unfortunately I do not have a ready solution but I think I can give you some tips since I spent some time thinking about this.

I do not know about TestNG but JUnit has ability to plugin your own test executor. I am sure that TestNG has appropriate functionality. So, find it and be familiar with it. Since you can control how your test is being invoked you can even do something else instead of invoking the test case's methods. For example call some remote API that will make the test to run remotely.

Obviously it can be web service that makes remote Agent (according to your suggestion) to run test on remote machine. It is fine, but I like agent-less or semi agent-less solutions more. What do I mean? If for example your remote machine is Unix you can perform SSH or Telnet connection and run command line. In this case you can create mvn or ant script, copy to remote machine using ssh and then run it. The script will run your tests. So it is almost agent less. You just require java installation and SSH support.

If remote machine is windows you can use either Telnet or WMI. So, if security is not an issue but you need cross platform support use Telnet.

Please do not hesitate to contact me if you need more assistance concerning SSH/Telnet.

爱格式化 2024-11-21 12:52:03

我自己没有做过(我的测试是本地的),但这里有一些可以工作的东西:

  • Cargo可用于运行服务器并将模块部署到其中
  • 运行测试可以使用 Cactus(相当旧)或 JUnitEE 来完成。在这两种情况下,您都可以使用 JUnit 实现测试并远程运行它们。另一种选择是编写测试以与服务器上的专用 API 交互,该 API 运行测试并返回报告。

I haven't done it myself (my tests are local), but here are some stuff that could work:

  • Cargo can be used to run a server and deploy modules into it
  • Running the tests can be done using Cactus (quite old) or JUnitEE. In bothe cases, you implement the tests using JUnit and run them remotely. Another option is to write the tests to interact with a dedicated API on your server which runs the tests and reports back.
坏尐絯℡ 2024-11-21 12:52:03

在我的解决方案中,我将启动本地 JNDI 服务器并添加一个远程对象(实现远程接口的对象),然后您可以使用该远程对象来同步本地 IDE 和远程服务器或其他地方。

In my solution, I will lunch a local JNDI server and add a remote object (an object implement the Remote interface), then you could use the remote object to sync your local IDE and remote server or other place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文