runTests()
Apex 単体テストを実行します。
構文
1RunTestsResult[] = binding.runTests(RunTestsRequest request);使用方法
堅牢で、エラーのないコードの開発を促進するため、Apex は単体テストの作成と実行をサポートします。単体テストは、コード内の特定の部分が正しく機能していることを確認するクラスメソッドです。単体テストのメソッドは引数を取らず、データベースへのデータの確定やメールの送信を行うこともなく、メソッド定義に testMethod キーワードまたは isTest アノテーションでフラグが付けられています。また、テストメソッドは、テストクラス (isTest アノテーションが付加されているクラス) で定義されている必要があります。このコールを使用して、Apex 単体テストを実行します。
このコールは、DebuggingHeader と SessionHeader をサポートしています。API の SOAP ヘッダーの詳細は、『SOAP API 開発者ガイド』を参照してください。
サンプルコード —Java
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public void runTestsSample() {
18 String sessionId = "sessionID goes here";
19 String url = "url goes here";
20 // Set the Apex stub with session ID received from logging in with the partner API
21 _SessionHeader sh = new _SessionHeader();
22 apexBinding.setHeader(
23 new ApexServiceLocator().getServiceName().getNamespaceURI(),
24 "SessionHeader", sh);
25 // Set the URL received from logging in with the partner API to the Apex stub
26 apexBinding._setProperty(ApexBindingStub.ENDPOINT_ADDRESS_PROPERTY, url);
27
28 // Set the debugging header
29 _DebuggingHeader dh = new _DebuggingHeader();
30 dh.setDebugLevel(LogType.Profiling);
31 apexBinding.setHeader(
32 new ApexServiceLocator().getServiceName().getNamespaceURI(),
33 "DebuggingHeader", dh);
34
35 long start = System.currentTimeMillis();
36 RunTestsRequest rtr = new RunTestsRequest();
37 rtr.setAllTests(true);
38 RunTestsResult res = null;
39 try {
40 res = apexBinding.runTests(rtr);
41 } catch (RemoteException e) {
42 System.out.println("An unexpected error occurred: " + e.getMessage());
43 }
44
45 System.out.println("Number of tests: " + res.getNumTestsRun());
46 System.out.println("Number of failures: " + res.getNumFailures());
47 if (res.getNumFailures() > 0) {
48 for (RunTestFailure rtf : res.getFailures()) {
49 System.out.println("Failure: " + (rtf.getNamespace() ==
50 null ? "" : rtf.getNamespace() + ".")
51 + rtf.getName() + "." + rtf.getMethodName() + ": "
52 + rtf.getMessage() + "\n" + rtf.getStackTrace());
53 }
54 }
55 if (res.getCodeCoverage() != null) {
56 for (CodeCoverageResult ccr : res.getCodeCoverage()) {
57 System.out.println("Code coverage for " + ccr.getType() +
58 (ccr.getNamespace() == null ? "" : ccr.getNamespace() + ".")
59 + ccr.getName() + ": "
60 + ccr.getNumLocationsNotCovered()
61 + " locations not covered out of "
62 + ccr.getNumLocations());
63
64 if (ccr.getNumLocationsNotCovered() > 0) {
65 for (CodeLocation cl : ccr.getLocationsNotCovered())
66 System.out.println("\tLine " + cl.getLine());
67 }
68 }
69 }
70 System.out.println("Finished in " +
71 (System.currentTimeMillis() - start) + "ms");
72} 引数
| 名前 | 型 | 説明 |
|---|---|---|
| request | RunTestsRequest | Apex 単体テストおよびこの要求に設定する必要のある項目の値を含む要求。 |