shardingtestcase.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2009 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Utility for sharding tests.
  16. *
  17. * Usage instructions:
  18. * <ol>
  19. * <li>Instead of writing your large test in foo_test.html, write it in
  20. * foo_test_template.html</li>
  21. * <li>Add a call to {@code goog.testing.ShardingTestCase.shardByFileName()}
  22. * near the top of your test, before any test cases or setup methods.</li>
  23. * <li>Symlink foo_test_template.html into different sharded test files
  24. * named foo_1of4_test.html, foo_2of4_test.html, etc, using `ln -s`.</li>
  25. * <li>Add the symlinks as foo_1of4_test.html.
  26. * In perforce, run the command `g4 add foo_1of4_test.html` followed
  27. * by `g4 reopen -t symlink foo_1of4_test.html` so that perforce treats the file
  28. * as a symlink
  29. * </li>
  30. * </ol>
  31. *
  32. * @author nicksantos@google.com (Nick Santos)
  33. */
  34. goog.setTestOnly('goog.testing.ShardingTestCase');
  35. goog.provide('goog.testing.ShardingTestCase');
  36. goog.require('goog.asserts');
  37. goog.require('goog.testing.TestCase');
  38. /**
  39. * A test case that runs tests in per-file shards.
  40. * @param {number} shardIndex Shard index for this page,
  41. * <strong>1-indexed</strong>.
  42. * @param {number} numShards Number of shards to split up test cases into.
  43. * @param {string=} opt_name The name of the test case.
  44. * @extends {goog.testing.TestCase}
  45. * @constructor
  46. * @final
  47. */
  48. goog.testing.ShardingTestCase = function(shardIndex, numShards, opt_name) {
  49. goog.testing.ShardingTestCase.base(this, 'constructor', opt_name);
  50. goog.asserts.assert(shardIndex > 0, 'Shard index should be positive');
  51. goog.asserts.assert(numShards > 0, 'Number of shards should be positive');
  52. goog.asserts.assert(shardIndex <= numShards, 'Shard index out of bounds');
  53. /**
  54. * @type {number}
  55. * @private
  56. */
  57. this.shardIndex_ = shardIndex;
  58. /**
  59. * @type {number}
  60. * @private
  61. */
  62. this.numShards_ = numShards;
  63. };
  64. goog.inherits(goog.testing.ShardingTestCase, goog.testing.TestCase);
  65. /**
  66. * Whether we've actually partitioned the tests yet. We may execute twice
  67. * ('Run again without reloading') without failing.
  68. * @type {boolean}
  69. * @private
  70. */
  71. goog.testing.ShardingTestCase.prototype.sharded_ = false;
  72. /**
  73. * Installs a runTests global function that goog.testing.JsUnit will use to
  74. * run tests, which will run a single shard of the tests present on the page.
  75. * @override
  76. */
  77. goog.testing.ShardingTestCase.prototype.runTests = function() {
  78. if (!this.sharded_) {
  79. var numTests = this.getCount();
  80. goog.asserts.assert(
  81. numTests >= this.numShards_,
  82. 'Must have at least as many tests as shards!');
  83. var shardSize = Math.ceil(numTests / this.numShards_);
  84. var startIndex = (this.shardIndex_ - 1) * shardSize;
  85. var endIndex = startIndex + shardSize;
  86. goog.asserts.assert(
  87. this.order == goog.testing.TestCase.Order.SORTED,
  88. 'Only SORTED order is allowed for sharded tests');
  89. this.setTests(this.getTests().slice(startIndex, endIndex));
  90. this.sharded_ = true;
  91. }
  92. // Call original runTests method to execute the tests.
  93. goog.testing.ShardingTestCase.base(this, 'runTests');
  94. };
  95. /**
  96. * Shards tests based on the test filename. Assumes that the filename is
  97. * formatted like 'foo_1of5_test.html'.
  98. * @param {string=} opt_name A descriptive name for the test case.
  99. */
  100. goog.testing.ShardingTestCase.shardByFileName = function(opt_name) {
  101. var path = window.location.pathname;
  102. var shardMatch = path.match(/_(\d+)of(\d+)_test\.(js|html)/);
  103. goog.asserts.assert(
  104. shardMatch, 'Filename must be of the form "foo_1of5_test.{js,html}"');
  105. var shardIndex = parseInt(shardMatch[1], 10);
  106. var numShards = parseInt(shardMatch[2], 10);
  107. var testCase =
  108. new goog.testing.ShardingTestCase(shardIndex, numShards, opt_name);
  109. goog.testing.TestCase.initializeTestRunner(testCase);
  110. };