Skip to content

Commit

Permalink
Remove usage of QiskitTestCase (qiskit-community#1285)
Browse files Browse the repository at this point in the history
This change removes the dependence on `QiskitTestCase`, replacing it
with a direct dependence on `unittest.TestCase` and
`testtools.TestCase`.

As with `QiskitTestCase`, the ability to run the tests based either on
`unittest.TestCase` or `testtools.TestCase` (a `unittest.TestCase`
subclass) is preserved. For qiskit-experiments, the ability is actually
restored because the timeout feature added in

[qiskit-community#1246](qiskit-community#1246)
had introduced a hard dependence on `testtools`.

Specific changes:

* Add `testtools` and `fixtures` to `requirements-dev.txt` as required
  test dependencies.
* Use `QE_USE_TESTTOOLS` environment variable to control whether tests
  are based on `testtools.TestCase` rather than checking if `testtools`
is installed.
* Remove some checks for test writing best practices. `QiskitTestCase`
  used extra code to ensure that `setUp` and other test class methods
always called their parents and that those methods are not called from
individual tests.  `testtools.TestCase` does these checks as well. Since
qiskit-experiments always uses `testtools` in CI, it can rely on
`testtools` for these checks and just not do them for the alternate
`unittest` execution.
* Generate `QiskitExperimentsTestCase` from a `create_base_test_case`
  function. This function allows the base test class to be generated
based on either `testtools.TestCase` or `unittest.TestCase` so that the
`unittest` variant can be tested for regressions even when the
`testtools` variant is enabled.

Closes
[qiskit-community#1282](qiskit-community#1282).
  • Loading branch information
wshanks authored and nkanazawa1989 committed Jan 17, 2024
1 parent 7d16410 commit b519fd9
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 190 deletions.
16 changes: 14 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ Note that tests will fail automatically if they do not finish execution within 6

#### STDOUT/STDERR and logging capture

When running tests in parallel using `stestr` either via tox, the Makefile (`make
test_ci`), or in CI, we set the env variable `QISKIT_TEST_CAPTURE_STREAMS`, which will
When running tests in parallel using `stestr` either via tox
or in CI, we set the env variable `QISKIT_TEST_CAPTURE_STREAMS`, which will
capture any text written to stdout, stderr, and log messages and add them as attachments
to the tests run so output can be associated with the test case it originated from.
However, if you run tests with `stestr` outside of these mechanisms, by default the
Expand All @@ -134,6 +134,18 @@ stdlib unittest runner, a similar result can be accomplished by using the
[`--buffer`](https://docs.python.org/3/library/unittest.html#command-line-options)
option (e.g. `python -m unittest discover --buffer ./test/python`).

#### Other testing related settings

The test code defines some environment variables that may occasionally be useful to set:

+ `TEST_TIMEOUT`: An integer representing the maximum time a test can take
before it is considered a failure.
+ `QE_USE_TESTTOOLS`: Set this variable to `FALSE`, `0`, or `NO` to have the
tests use `unittest.TestCase` as the base class. Otherwise, the default is
`testtools.TestCase` which is an extension of `unittest.TestCase`. In some
situations, a developer may wish to use a workflow that is not compatible with
the `testtools` extensions.

### Code style

The qiskit-experiments repository uses `black` for code formatting and style and
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
qiskit-terra>=0.45.0
black~=22.0
fixtures
stestr
testtools
pylint~=3.0.2
astroid~=3.0.1 # Must be kept aligned to what pylint wants
jinja2==3.0.3
Expand Down
Loading

0 comments on commit b519fd9

Please sign in to comment.