この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

runTests()

Apex 単体テストを実行します。

構文

1RunTestsResult[] = binding.runTests(RunTestsRequest request);

使用方法

堅牢で、エラーのないコードの開発を促進するため、Apex は単体テストの作成と実行をサポートします。単体テストは、コード内の特定の部分が正しく機能していることを確認するクラスメソッドです。単体テストのメソッドは引数を取らず、データベースにデータをコミットせず、メールを送信しません。このメソッドは、メソッド定義内で @isTest アノテーションでフラグ付けされます。単体テストメソッドは、テストクラス (@isTest アノテーションが付加されているクラス) で定義されている必要があります。このコールを使用して、Apex 単体テストを実行します。

このコールは、DebuggingHeader と SessionHeader をサポートしています。API の SOAP ヘッダーについての詳細は、SOAP API 開発者ガイドを参照してください。

サンプルコード —Java

1public void runTestsSample() {
2   String sessionId = "sessionID goes here";
3   String url = "url goes here";
4   // Set the Apex stub with session ID received from logging in with the partner API
5   _SessionHeader sh = new _SessionHeader();
6   apexBinding.setHeader(
7      new ApexServiceLocator().getServiceName().getNamespaceURI(),
8      "SessionHeader", sh);
9   // Set the URL received from logging in with the partner API to the Apex stub
10   apexBinding._setProperty(ApexBindingStub.ENDPOINT_ADDRESS_PROPERTY, url);
11
12   // Set the debugging header
13   _DebuggingHeader dh = new _DebuggingHeader();
14   dh.setDebugLevel(LogType.Profiling);
15   apexBinding.setHeader(
16      new ApexServiceLocator().getServiceName().getNamespaceURI(),
17      "DebuggingHeader", dh);
18
19   long start = System.currentTimeMillis();
20   RunTestsRequest rtr = new RunTestsRequest();
21   rtr.setAllTests(true);
22   RunTestsResult res = null;
23   try {
24      res = apexBinding.runTests(rtr);
25   } catch (RemoteException e) {
26      System.out.println("An unexpected error occurred: " + e.getMessage());
27   }
28
29   System.out.println("Number of tests: " + res.getNumTestsRun());
30   System.out.println("Number of failures: " + res.getNumFailures());
31   if (res.getNumFailures() > 0) {
32      for (RunTestFailure rtf : res.getFailures()) {
33         System.out.println("Failure: " + (rtf.getNamespace() ==
34         null ? "" : rtf.getNamespace() + ".")
35         + rtf.getName() + "." + rtf.getMethodName() + ": "
36         + rtf.getMessage() + "\n" + rtf.getStackTrace());
37      }
38   }
39   if (res.getCodeCoverage() != null) {
40      for (CodeCoverageResult ccr : res.getCodeCoverage()) {
41         System.out.println("Code coverage for " + ccr.getType() +
42         (ccr.getNamespace() == null ? "" : ccr.getNamespace() + ".")
43         + ccr.getName() + ": "
44         + ccr.getNumLocationsNotCovered()
45         + " locations not covered out of "
46         + ccr.getNumLocations());
47   
48      if (ccr.getNumLocationsNotCovered() > 0) {
49         for (CodeLocation cl : ccr.getLocationsNotCovered())
50            System.out.println("\tLine " + cl.getLine());
51         }
52      }
53   }
54   System.out.println("Finished in " +
55   (System.currentTimeMillis() - start) + "ms");
56}

引数

名前 説明
request RunTestsRequest Apex 単体テストおよびこの要求に設定する必要のある項目の値を含む要求。