Since only one junit test has been written, and the test project logic is complex, the following are just a few of the solutions to the problems encountered in the test.
Web server: Linux
Client: Linux/windows (as long as you can execute Java program, test Java-jar Test_client.jar)
Problem
1, because the test project is different, you need to perform deployment of different test environments, how to perform deployment operations on the client?
A: You can remotely execute commands from the server in Java via SSH, for example. (Note, I quote is the Internet casually looking for the bag, of course you can also write one yourself)
import ch.ethz.ssh2.connection;import ch.ethz.ssh2.session; @After public void teardown () throws Exception { try { Connection conn = new connection (TESTCONFIG.SERVER_IP); conn.connect (); conn.authenticatewithpassword (testconfig.username, testconfig.password); Session session = Conn.opensession (); string Execcommandstring = testconfig.delete_Application; session.execcommand ( execcommandstring); thread.sleep ( Testconfig.delete_application_sleeptime); session.close (); conn.close (); } catch (exception e) { system.out.println ("delete application error!"); throw new exception (); } }
2, how to imitate the browser to visit the site?
import javax.ws.rs.client.client;import javax.ws.rs.client.clientbuilder;import Javax.ws.rs.client.webtarget;// run test. client client = clientbuilder.newclient (); webtarget target = client.target (Testconfig.testurl); string response = null; for (int i = 0; i < testconfig.request_times; i++) { // if there ' s only request,response may be 404. response = target.request (). Get(String.class). toString (); } client.close ();
3, junit4 default test all case using @test annotations, if you want to decide what to test according to the test environment?
Import junit.framework.testsuite;public class testall { public static void main (String[] args) { try { //initialization, get parameters, etc... testconfig.init (); } catch (exception e) { system.out.println ("Testconfig init fail."); system.out.println ("Test over."); system.exit (1); } testsuite suite = new testsuite (); //Judging Environment xff string xff = Testutil.getoriginxff (); if (XFF != null) { switch (XFF) { case TestConfig.XFF2TT: //testconfig.xff2tt_tests is an array of strings, Includes the serial number that needs to be tested in the current environment. for (int i = 0; i < testconfig.xff2tt_tests.length; i++) { Suite.addtest (New xff_2_tt ("Test" &nbsP; + testconfig.xff2tt_tests[i])); } break; case TestConfig.XFF2TF: //testconfig.xff2tf_tests is an array of strings, Includes the serial number that needs to be tested in the current environment. for (int i = 0; i < testconfig.xff2tf_tests.length; i++) { Suite.addtest (NEW&NBSP;XFF_2_TF ("Test" &nbSp; + testconfig.xff2tf_ Tests[i]); } break; default: break; } } else { system.out.println ("Did not find Any tests fit with the environment. "); system.out.println ("Test over."); &nbsP System.exit (0); } testutil.createbuildpack (); junit.textui.testrunner.run ( suite); testutil.deletebuildpack (); }}
4, how to get configuration parameters, and users directly modify the values in the file, without the need to modify the test program?
Create the configuration file in the same directory as the test jar package, as shown below.
#server系统配置 #====================================================== #分别表示主机名, the server's IP, user name, and password must be specified. Host = Remoteserver.ip = 0.0.0.0username = Rootpassword = toor#======================================================
Then get the parameters in the program, as shown below.
public static void init () throws Exception { String propertiesFile = null; Properties properties = null; Try { propertiesfile = "Testconfig.properties"; properties = new properties (); properties.load (New fileinputstream (propertiesFile)); } catch (Exception e) { system.Out.println ("Load propertiesfile fail."); throw new exception (); } try { host = properties.getproperty ("HOST", ""); SERVER_IP = Properties.getproperty ("Server.ip"); username = properties.getproperty ("USERNAME"); password = properties.gEtproperty ("password"); if ("". Equals (HOST) | | ". Equals (server_ip) | | ". Equals (USERNAME) | | ". Equals (PASSWORD)) { throw new exception (); } } catch (exception e) { system.out.println ("There are no configuration parameters or parameter analysis error. "); throw new exception (); }
This article is from the "Night" blog, be sure to keep this source http://icyore.blog.51cto.com/8486958/1701343
"Junit" Web test