简体   繁体   English

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

[英]Run TestNG/JUnit Integration Tests on Remote Server from within IDE

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. 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).他们需要完整的集成服务器环境——不仅仅是 JavaEE 容器相关的东西(=> 没有 Arquillian 也没有 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.现在我希望能够从我的 IDE (Eclipse) 中运行这些测试——最好使用 TestNG 插件——但是当我启动它们时,它们实际上应该在远程集成服务器上运行。

Is it possible to launch integration tests on a remote server from within my IDE?是否可以从我的 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.但正如我所说,如果它从 TestNG 插件内部运行,那就太好了。

Do I need some kind of workaround, for example Ant scripts (hopefully not) or some Maven magic?我是否需要某种解决方法,例如 Ant 脚本(希望不是)或一些 Maven 魔法? What are the best practices?最佳实践是什么?

I know I could also create Webservices for my application, then I can call them from local unit tests.我知道我还可以为我的应用程序创建 Web 服务,然后我可以从本地单元测试中调用它们。 But I'd like to know if there are also possibilities without Webservices.但是我想知道没有Webservices是否也有可能。

How you will accomplish this depends a lot on what integration server and IDE you're using, and what you're application is.您将如何实现这一点在很大程度上取决于您使用的集成服务器和 IDE,以及您的应用程序是什么。 I'll assume you're using Eclipse;我假设您使用的是 Eclipse; and I'll assume you're using Jenkins , since it will IMO be the easiest to get it to do what you want.我会假设你正在使用Jenkins ,因为 IMO 最容易让它做你想做的事。

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).在 Jenkins 中设置一个名为“集成测试”的作业(名称无关紧要)。 Configure it to run the tests you want to be able to run from your IDE, and to allow remote machines to trigger builds.将其配置为运行您希望能够从 IDE 运行的测试,并允许远程计算机触发构建。
  2. Make this job a parameterized job, and add a file parameter.将此作业设为参数化作业,并添加文件参数。 Lets suppose this is called 'TestingProgram'.让我们假设这被称为“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.使集成测试作业使用 TestingProgram 文件来运行测试。 If you're program requires more than one file, than assume this will be a zip file containing all of the necessary files.如果您的程序需要多个文件,则假设这将是一个包含所有必要文件的 zip 文件。
  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)配置您的 Eclipse 项目以生成集成测试作业所期望的相同文件(我假设这已经发生,因为它可能正在构建您程序的某种 bin 版本)

Now comes the slightly more complicated part of hooking up Eclipse to Jenkins.现在是连接 Eclipse 到 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如果您的程序需要多个文件,而 eclipse 还没有这样做,则需要 zip 这些文件
  2. It will need to take the file and read it into some variable, and then base64 encode it.它需要获取文件并将其读入某个变量,然后 base64 对其进行编码。 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让我们假设这个文件被读入一个名为$programFile的变量
  3. It will need to send a HTTP request to http://<your-jenkins-server>:8080/<integrated-testing-job-name>/buildwithparameters?TestingProgram=$programFile它需要向http://<your-jenkins-server>:8080/<integrated-testing-job-name>/buildwithparameters?TestingProgram=$programFile发送 HTTP 请求

You can read up more on triggering Jenkins remote builds with parameters in the jenkins docs .您可以阅读更多关于使用jenkins 文档中的参数触发 Jenkins 远程构建的信息。

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.鉴于您希望最终将其合并到 IDE 中,似乎最合乎逻辑的选择是 ant 脚本或 Eclipse 插件。 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.两者都不会太复杂 - ant 脚本只会执行这些步骤,您可以将 ant 脚本导入 Eclipse 项目,该项目专门用于在项目中运行时执行测试 - 并且在项目中运行时,只需添加一个菜单项即可该项目的上述步骤。

NOTE: There are actually several different ways to trigger a build with parameters using Jenkins.注意:实际上有几种不同的方法可以使用 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.例如,您可以执行 POST 请求,使用 json 传递参数,如我链接到的 jenkins 文档中所述,使用 Jenkins CLI 等。并非所有文件都使用它们,但它们都可以与参数一起使用非常相似的方式 - 作为自定义脚本中的一个步骤,您将在 Jenkins 上执行远程构建,并传递您要用于测试的文件。 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.如果不是这种情况,您可能想要改为执行 POST 请求。 If you run into problems using one method, it should be pretty easy to switch it to use a different method that works better.如果您在使用一种方法时遇到问题,应该很容易将其切换为使用另一种效果更好的方法。

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. 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.

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.我不知道 TestNG 但 JUnit 能够插入您自己的测试执行器。 I am sure that TestNG has appropriate functionality.我确信 TestNG 具有适当的功能。 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.例如调用一些远程 API 将使测试远程运行。

Obviously it can be web service that makes remote Agent (according to your suggestion) to run test on remote machine.显然,它可以是 web 服务使远程代理(根据您的建议)在远程机器上运行测试。 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.例如,如果您的远程计算机是 Unix 您可以执行 SSH 或 Telnet 连接并运行命令行。 In this case you can create mvn or ant script, copy to remote machine using ssh and then run it.在这种情况下,您可以创建 mvn 或 ant 脚本,使用 ssh 复制到远程计算机,然后运行它。 The script will run your tests.该脚本将运行您的测试。 So it is almost agent less.所以几乎是少了代理。 You just require java installation and SSH support.您只需要 java 安装和 SSH 支持。

If remote machine is windows you can use either Telnet or WMI.如果远程机器是 windows,您可以使用 Telnet 或 WMI。 So, if security is not an issue but you need cross platform support use Telnet.因此,如果安全不是问题,但您需要跨平台支持,请使用 Telnet。

Please do not hesitate to contact me if you need more assistance concerning SSH/Telnet.如果您需要有关 SSH/Telnet 的更多帮助,请随时与我联系。

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 Cargo可用于运行服务器并将模块部署到其中
  • Running the tests can be done using Cactus (quite old) or JUnitEE.可以使用 Cactus(相当老的)或 JUnitEE 来运行测试。 In bothe cases, you implement the tests using JUnit and run them remotely.在这两种情况下,您都使用 JUnit 实现测试并远程运行它们。 Another option is to write the tests to interact with a dedicated API on your server which runs the tests and reports back.另一种选择是编写测试以与运行测试并返回报告的服务器上的专用 API 进行交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM