Python: How to piggyback on existing tests when developing third party packages
<p>My <a href="https://github.com/tomchristie/django-rest-framework/pull/4540" rel="nofollow">PR</a> on django-rest-framework to add in a "hybrid pagination" got rejected reason being better to be in a 3rd party package. </p>

<p>So I went ahead and created the package structure but got stuck in creating the test, if you have a look at the <a href="https://github.com/tomchristie/django-rest-framework/pull/4540/files" rel="nofollow">PR files changed</a>, my new tests are merely extending the existing tests and changed to use my new pagination class.</p>

<pre><code> +class TestCombinedPaginationPageNumber(TestPageNumberPagination):
 +    def setup(self):
 +        class ExamplePagination(pagination.HybridPagination):
 +            page_size = 5
 +
 +        self.pagination = ExamplePagination()
 +        self.queryset = range(1, 101)
 +
 +
 +class TestCombinedPaginationLimitOffset(TestLimitOffset):
 +    def setup(self):
 +        class ExamplePagination(pagination.HybridPagination):
 +            default_limit = 10
 +            max_limit = 15
 +
 +        self.pagination = ExamplePagination()
 +        self.queryset = range(1, 101)
</code></pre>

<p>I am having trouble working out a way to piggyback these tests in my own 3rd party tests, I can't extend it remotely since installing the package doesn't include test files. I tried copying the particular <code>test_pagination.py</code> file but getting a lot of errors.</p>

<p>Correct that you won't be able to include the tests from the <code>pip install</code> package. You'll need to clone whichever bits of test case you want to replicate locally.</p>

<blockquote>
  <p>I tried copying the particular test_pagination.py file but getting a lot of errors.</p>
</blockquote>

<p>I'd suggest starting off small. Take a single test case that you want to replicate. Copy <em>just that one</em>, and also any imports it relies on.</p>

<p>More generally you should probably just try to exclusively test the bits that your package adds, rather than re-testing REST framework's behavior. Eg for your "switch between pagination styles", don't aim to test the pagination itself, but rather the switching behavior.</p>

<p>Hope that helps. If you've any issues with resolving specific errors while writing the test case please do shout out on the REST framework mailing list.</p>

python django