将命令行参数传递给以编程方式运行的 JUnit 测试用例
我正在尝试从 Java 类运行 JUnit 测试:
JUnitCore core = new JUnitCore();
core.addListener(new RunListener());
core.run(classToRun);
问题是我的 JUnit 测试需要一个当前在 JUnit 测试本身中硬编码的数据库连接。
我正在寻找一种以编程方式运行 JUnit 测试的方法(上面),但将我在运行测试的 Java 类中创建的数据库连接传递给它,而不是在 JUnit 类中硬编码。
基本上类似于
JUnitCore core = new JUnitCore();
core.addListener(new RunListener());
core.addParameters(java.sql.Connection);
core.run(classToRun);
then 在 classToRun 中:
@Test
Public void Test1(Connection dbConnection){
Statement st = dbConnection.createStatement();
ResultSet rs = st.executeQuery("select total from dual");
rs.next();
String myTotal = rs.getString("TOTAL");
//btw my tests are selenium testcases:)
selenium.isTextPresent(myTotal);
}
我知道 @Parameters,但它在这里似乎不适用,因为它更多的是使用不同的值多次运行相同的测试用例。我希望所有测试用例共享一个数据库连接,我通过配置文件将其传递到我的 java 客户端,然后运行这些测试用例(也通过配置文件传递)。
这可能吗?
PS 我知道这似乎是一种奇怪的做事方式。
I am attempting to run a JUnit Test from a Java Class with:
JUnitCore core = new JUnitCore();
core.addListener(new RunListener());
core.run(classToRun);
Problem is my JUnit test requires a database connection that is currently hardcoded in the JUnit test itself.
What I am looking for is a way to run the JUnit test programmatically(above) but pass a database connection to it that I create in my Java Class that runs the test, and not hardcoded within the JUnit class.
Basically something like
JUnitCore core = new JUnitCore();
core.addListener(new RunListener());
core.addParameters(java.sql.Connection);
core.run(classToRun);
Then within the classToRun:
@Test
Public void Test1(Connection dbConnection){
Statement st = dbConnection.createStatement();
ResultSet rs = st.executeQuery("select total from dual");
rs.next();
String myTotal = rs.getString("TOTAL");
//btw my tests are selenium testcases:)
selenium.isTextPresent(myTotal);
}
I know about The @Parameters, but it doesn't seem applicable here as it is more for running the same test case multiple times with differing values. I want all of my test cases to share a database connection that I pass in through a configuration file to my java client that then runs those test cases (also passed in through the configuration file).
Is this possible?
P.S. I understand this seems like an odd way of doing things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 java 系统属性来实现此目的。
只需在 junit 命令行中使用
-Dconnectionstring=foobar
传递您需要的内容,或者使用系统属性的 java api 以编程方式设置此属性,即System.setProperty(String name, String value)
和System.getProperty(String name)
。在测试中,您可以使用
@Before
或@BeforeClass
基于此属性设置公共对象,具体取决于您是否要为每个测试运行一次设置(在这种情况下,您可以使用类成员)或每个套件一次(然后使用静态成员)。您甚至可以使用所有测试用例扩展的抽象类来分解此行为。
You can use java system properties to achieve this.
Simply pass what you need with
-Dconnectionstring=foobar
in the junit command line, or use the java api for system properties to set this programmatically, withSystem.setProperty(String name, String value)
, andSystem.getProperty(String name)
.In your tests, you can use the
@Before
or@BeforeClass
to set up common objects based on this property, pending on whether you want to run the setup once for each test (in which case you can use class members) or once for each suite (and then use static members).You can even factorize this behavior by using an abstract class which all your test cases extends.