blob: 3fec119b8218fe95c8c59e507306ca8955a27472 [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="The Apache Cassandra database is the right choice when you need scalability and high availability without compromising performance. Linear scalability and proven fault-tolerance on commodity hardware or cloud infrastructure make it the perfect platform for mission-critical data. Cassandra's support for replicating across multiple datacenters is best-in-class, providing lower latency for your users and the peace of mind of knowing that you can survive regional outages.
">
<meta name="keywords" content="cassandra, apache, apache cassandra, distributed storage, key value store, scalability, bigtable, dynamo" />
<meta name="robots" content="index,follow" />
<meta name="language" content="en" />
<title>Documentation</title>
<link rel="canonical" href="http://cassandra.apache.org/doc/4.0-alpha5/development/testing.html">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="./../../../css/style.css">
<link rel="stylesheet" href="./../../../css/sphinx.css">
<link rel="top" title="Apache Cassandra Documentation v4.0-alpha5" href="../index.html"/> <link rel="up" title="Contributing to Cassandra" href="index.html"/> <link rel="next" title="Contributing Code Changes" href="patches.html"/> <link rel="prev" title="Building and IDE Integration" href="ide.html"/>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<link type="application/atom+xml" rel="alternate" href="http://cassandra.apache.org/feed.xml" title="Apache Cassandra Website" />
</head>
<body>
<!-- breadcrumbs -->
<div class="topnav">
<div class="container breadcrumb-container">
<ul class="breadcrumb">
<li>
<div class="dropdown">
<img class="asf-logo" src="./../../../img/asf_feather.png" />
<a data-toggle="dropdown" href="#">Apache Software Foundation <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
<li><a href="http://www.apache.org">Apache Homepage</a></li>
<li><a href="http://www.apache.org/licenses/">License</a></li>
<li><a href="http://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
<li><a href="http://www.apache.org/foundation/thanks.html">Thanks</a></li>
<li><a href="http://www.apache.org/security/">Security</a></li>
</ul>
</div>
</li>
<li><a href="./../../../">Apache Cassandra</a></li>
<li><a href="./../../../doc/latest/">Documentation</a></li>
<li><a href="./">Contributing to Cassandra</a></li>
<li>Testing</li>
</ul>
</div>
<!-- navbar -->
<nav class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#cassandra-menu" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="./../../../"><img src="./../../../img/cassandra_logo.png" alt="Apache Cassandra logo" /></a>
</div><!-- /.navbar-header -->
<div id="cassandra-menu" class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li><a href="./../../../">Home</a></li>
<li><a href="./../../../download/">Download</a></li>
<li><a href="./../../../doc/latest/">Documentation</a></li>
<li><a href="./../../../community/">Community</a></li>
<li>
<a href="./../../../blog/">Blog</a>
</li>
</ul>
</div><!-- /#cassandra-menu -->
</div>
</nav><!-- /.navbar -->
</div><!-- /.topnav -->
<div class="container-fluid">
<div class="row">
<div class="col-md-3">
<div class="doc-navigation">
<div class="doc-menu" role="navigation">
<div class="navbar-header">
<button type="button" class="pull-left navbar-toggle" data-toggle="collapse" data-target=".sidebar-navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse sidebar-navbar-collapse">
<form id="doc-search-form" class="navbar-form" action="../search.html" method="get" role="search">
<div class="form-group">
<input type="text" size="30" class="form-control input-sm" name="q" placeholder="Search docs">
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</div>
</form>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../getting_started/index.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="../new/index.html">New Features in Apache Cassandra 4.0</a></li>
<li class="toctree-l1"><a class="reference internal" href="../architecture/index.html">Architecture</a></li>
<li class="toctree-l1"><a class="reference internal" href="../cql/index.html">The Cassandra Query Language (CQL)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../data_modeling/index.html">Data Modeling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../configuration/index.html">Configuring Cassandra</a></li>
<li class="toctree-l1"><a class="reference internal" href="../operating/index.html">Operating Cassandra</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tools/index.html">Cassandra Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../troubleshooting/index.html">Troubleshooting</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Contributing to Cassandra</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="gettingstarted.html">Getting Started</a></li>
<li class="toctree-l2"><a class="reference internal" href="ide.html">Building and IDE Integration</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Testing</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#unit-testing">Unit Testing</a></li>
<li class="toctree-l3"><a class="reference internal" href="#dtests">DTests</a></li>
<li class="toctree-l3"><a class="reference internal" href="#performance-testing">Performance Testing</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="patches.html">Contributing Code Changes</a></li>
<li class="toctree-l2"><a class="reference internal" href="code_style.html">Code Style</a></li>
<li class="toctree-l2"><a class="reference internal" href="how_to_review.html">Review Checklist</a></li>
<li class="toctree-l2"><a class="reference internal" href="how_to_commit.html">How-to Commit</a></li>
<li class="toctree-l2"><a class="reference internal" href="documentation.html">Working on Documentation</a></li>
<li class="toctree-l2"><a class="reference internal" href="ci.html">Jenkins CI Environment</a></li>
<li class="toctree-l2"><a class="reference internal" href="dependencies.html">Dependency Management</a></li>
<li class="toctree-l2"><a class="reference internal" href="release_process.html">Release Process</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../faq/index.html">Frequently Asked Questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../plugins/index.html">Third-Party Plugins</a></li>
<li class="toctree-l1"><a class="reference internal" href="../bugs.html">Reporting Bugs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contactus.html">Contact us</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="col-md-8">
<div class="content doc-content">
<div class="content-container">
<div class="section" id="testing">
<span id="id1"></span><h1>Testing<a class="headerlink" href="#testing" title="Permalink to this headline"></a></h1>
<p>Creating tests is one of the most important and also most difficult parts of developing Cassandra. There are different ways to test your code depending on what you’re working on.</p>
<div class="section" id="unit-testing">
<h2>Unit Testing<a class="headerlink" href="#unit-testing" title="Permalink to this headline"></a></h2>
<p>The most simple way to test code in Cassandra is probably by writing a unit test. Cassandra uses JUnit as a testing framework and test cases can be found in the <code class="docutils literal notranslate"><span class="pre">test/unit</span></code> directory. Ideally you’d be able to create a unit test for your implementation that would exclusively cover the class you created (the unit under test). Unfortunately this is not always possible and Cassandra doesn’t have a very mock friendly code base. Often you’ll find yourself in a situation where you have to make use of an embedded Cassandra instance that you’ll be able to interact with in your test. If you want to make use of CQL in your test, you can simply extend CQLTester and use some of the convenient helper methods such as in the following example.</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="nd">@Test</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">testBatchAndList</span><span class="p">()</span> <span class="kd">throws</span> <span class="n">Throwable</span>
<span class="p">{</span>
<span class="n">createTable</span><span class="p">(</span><span class="s">&quot;CREATE TABLE %s (k int PRIMARY KEY, l list&lt;int&gt;)&quot;</span><span class="p">);</span>
<span class="n">execute</span><span class="p">(</span><span class="s">&quot;BEGIN BATCH &quot;</span> <span class="o">+</span>
<span class="s">&quot;UPDATE %1$s SET l = l +[ 1 ] WHERE k = 0; &quot;</span> <span class="o">+</span>
<span class="s">&quot;UPDATE %1$s SET l = l + [ 2 ] WHERE k = 0; &quot;</span> <span class="o">+</span>
<span class="s">&quot;UPDATE %1$s SET l = l + [ 3 ] WHERE k = 0; &quot;</span> <span class="o">+</span>
<span class="s">&quot;APPLY BATCH&quot;</span><span class="p">);</span>
<span class="n">assertRows</span><span class="p">(</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;SELECT l FROM %s WHERE k = 0&quot;</span><span class="p">),</span>
<span class="n">row</span><span class="p">(</span><span class="n">list</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)));</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Unit tests can be run from the command line using the <code class="docutils literal notranslate"><span class="pre">ant</span> <span class="pre">test</span></code> command, <code class="docutils literal notranslate"><span class="pre">ant</span> <span class="pre">test</span> <span class="pre">-Dtest.name=&lt;simple_classname&gt;</span></code> to execute a test suite or <code class="docutils literal notranslate"><span class="pre">ant</span> <span class="pre">testsome</span> <span class="pre">-Dtest.name=&lt;FQCN&gt;</span> <span class="pre">-Dtest.methods=&lt;testmethod1&gt;[,testmethod2]</span></code> for individual tests. For example, to run all test methods in the <code class="docutils literal notranslate"><span class="pre">org.apache.cassandra.cql3.SimpleQueryTest</span></code> class, you would run:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>ant test -Dtest.name=SimpleQueryTest
</pre></div>
</div>
<p>To run only the <code class="docutils literal notranslate"><span class="pre">testStaticCompactTables()</span></code> test method from that class, you would run:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>ant testsome -Dtest.name=org.apache.cassandra.cql3.SimpleQueryTest -Dtest.methods=testStaticCompactTables
</pre></div>
</div>
<p>If you see an error like this:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Throws: cassandra-trunk/build.xml:1134: taskdef A class needed by class org.krummas.junit.JStackJUnitTask cannot be found:
org/apache/tools/ant/taskdefs/optional/junit/JUnitTask using the classloader
AntClassLoader[/.../cassandra-trunk/lib/jstackjunit-0.0.1.jar]
</pre></div>
</div>
<p>You will need to install the ant-optional package since it contains the <code class="docutils literal notranslate"><span class="pre">JUnitTask</span></code> class.</p>
<div class="section" id="long-running-tests">
<h3>Long running tests<a class="headerlink" href="#long-running-tests" title="Permalink to this headline"></a></h3>
<p>Test that consume a significant amount of time during execution can be found in the <code class="docutils literal notranslate"><span class="pre">test/long</span></code> directory and executed as a regular JUnit test or standalone program. Except for the execution time, there’s nothing really special about them. However, ant will execute tests under <code class="docutils literal notranslate"><span class="pre">test/long</span></code> only when using the <code class="docutils literal notranslate"><span class="pre">ant</span> <span class="pre">long-test</span></code> target.</p>
</div>
</div>
<div class="section" id="dtests">
<h2>DTests<a class="headerlink" href="#dtests" title="Permalink to this headline"></a></h2>
<p>One way of doing integration or system testing at larger scale is by using <a class="reference external" href="https://github.com/apache/cassandra-dtest">dtest</a>, which stands for “Cassandra Distributed Tests”. The idea is to automatically setup Cassandra clusters using various configurations and simulate certain use cases you want to test. This is done using Python scripts and <code class="docutils literal notranslate"><span class="pre">ccmlib</span></code> from the <a class="reference external" href="https://github.com/pcmanus/ccm">ccm</a> project. Dtests will setup clusters using this library just as you do running ad-hoc <code class="docutils literal notranslate"><span class="pre">ccm</span></code> commands on your local machine. Afterwards dtests will use the <a class="reference external" href="http://datastax.github.io/python-driver/installation.html">Python driver</a> to interact with the nodes, manipulate the file system, analyze logs or mess with individual nodes.</p>
<p>Using dtests helps us to prevent regression bugs by continually executing tests on the <a class="reference external" href="https://builds.apache.org/">CI server</a> against new patches. Committers will be able to set up build branches there and your reviewer may use the CI environment to run tests for your patch. Read more on the motivation behind continuous integration <a class="reference external" href="http://www.datastax.com/dev/blog/cassandra-testing-improvements-for-developer-convenience-and-confidence">here</a>.</p>
<p>The best way to learn how to write dtests is probably by reading the introduction “<a class="reference external" href="http://www.datastax.com/dev/blog/how-to-write-a-dtest">How to Write a Dtest</a>” and by looking at existing, recently updated tests in the project. New tests must follow certain <a class="reference external" href="https://github.com/apache/cassandra-dtest/blob/master/CONTRIBUTING.md">style conventions</a> that are being checked before accepting contributions. In contrast to Cassandra, dtest issues and pull-requests are managed on github, therefor you should make sure to link any created dtests in your Cassandra ticket and also refer to the ticket number in your dtest PR.</p>
<p>Creating a good dtest can be tough, but it should not prevent you from submitting patches! Please ask in the corresponding JIRA ticket how to write a good dtest for the patch. In most cases a reviewer or committer will able to support you, and in some cases they may offer to write a dtest for you.</p>
</div>
<div class="section" id="performance-testing">
<h2>Performance Testing<a class="headerlink" href="#performance-testing" title="Permalink to this headline"></a></h2>
<p>Performance tests for Cassandra are a special breed of tests that are not part of the usual patch contribution process. In fact you can contribute tons of patches to Cassandra without ever running performance tests. They are important however when working on performance improvements, as such improvements must be measurable.</p>
<div class="section" id="cassandra-stress-tool">
<h3>Cassandra Stress Tool<a class="headerlink" href="#cassandra-stress-tool" title="Permalink to this headline"></a></h3>
<p>See <a class="reference internal" href="../tools/cassandra_stress.html#cassandra-stress"><span class="std std-ref">Cassandra Stress</span></a></p>
</div>
<div class="section" id="cstar-perf">
<h3>cstar_perf<a class="headerlink" href="#cstar-perf" title="Permalink to this headline"></a></h3>
<p>Another tool available on github is <a class="reference external" href="https://github.com/datastax/cstar_perf">cstar_perf</a> that can be used for intensive performance testing in large clusters or locally. Please refer to the project page on how to set it up and how to use it.</p>
</div>
<div class="section" id="circleci">
<h3>CircleCI<a class="headerlink" href="#circleci" title="Permalink to this headline"></a></h3>
<p>Cassandra ships with a default <a class="reference external" href="https://circleci.com">CircleCI</a> configuration, to enable running tests on your branches, you need to go the CircleCI website, click “Login” and log in with your github account. Then you need to give CircleCI permission to watch your repositories. Once you have done that, you can optionally configure CircleCI to run tests in parallel - click “Projects”, then your github account and then click the settings for the project. If you leave the parallelism at 1 for Cassandra, only <code class="docutils literal notranslate"><span class="pre">ant</span> <span class="pre">eclipse-warnings</span></code> and <code class="docutils literal notranslate"><span class="pre">ant</span> <span class="pre">test</span></code> will be run. If you up the parallelism to 4, it also runs <code class="docutils literal notranslate"><span class="pre">ant</span> <span class="pre">long-test</span></code>, <code class="docutils literal notranslate"><span class="pre">ant</span> <span class="pre">test-compression</span></code> and <code class="docutils literal notranslate"><span class="pre">ant</span> <span class="pre">stress-test</span></code></p>
</div>
</div>
</div>
<div class="doc-prev-next-links" role="navigation" aria-label="footer navigation">
<a href="patches.html" class="btn btn-default pull-right " role="button" title="Contributing Code Changes" accesskey="n">Next <span class="glyphicon glyphicon-circle-arrow-right" aria-hidden="true"></span></a>
<a href="ide.html" class="btn btn-default" role="button" title="Building and IDE Integration" accesskey="p"><span class="glyphicon glyphicon-circle-arrow-left" aria-hidden="true"></span> Previous</a>
</div>
</div>
</div>
</div>
</div>
</div>
<hr />
<footer>
<div class="container">
<div class="col-md-4 social-blk">
<span class="social">
<a href="https://twitter.com/cassandra"
class="twitter-follow-button"
data-show-count="false" data-size="large">Follow @cassandra</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<a href="https://twitter.com/intent/tweet?button_hashtag=cassandra"
class="twitter-hashtag-button"
data-size="large"
data-related="ApacheCassandra">Tweet #cassandra</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
</span>
<a class="subscribe-rss icon-link" href="/feed.xml" title="Subscribe to Blog via RSS">
<span><i class="fa fa-rss"></i></span>
</a>
</div>
<div class="col-md-8 trademark">
<p>&copy; 2016 <a href="http://apache.org">The Apache Software Foundation</a>.
Apache, the Apache feather logo, and Apache Cassandra are trademarks of The Apache Software Foundation.
<p>
</div>
</div><!-- /.container -->
</footer>
<!-- Javascript. Placed here so pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="./../../../js/underscore-min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="./../../../js/doctools.js"></script>
<script src="./../../../js/searchtools.js"></script>
<script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: "", VERSION: "", COLLAPSE_INDEX: false, FILE_SUFFIX: ".html", HAS_SOURCE: false, SOURCELINK_SUFFIX: ".txt" }; </script>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
try {
var pageTracker = _gat._getTracker("UA-11583863-1");
pageTracker._trackPageview();
} catch(err) {}
</script>
</body>
</html>