-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove usage of QiskitTestCase #1285
Conversation
I think this will keep the parts of In the current form, I didn't copy over the code from |
test/base.py
Outdated
|
||
@classmethod | ||
def setUpClass(cls): | ||
"""Set-up test class.""" | ||
super().setUpClass() | ||
|
||
warnings.filterwarnings("error", category=DeprecationWarning) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
QiskitTestCase
hard codes many exceptions to this rule. One thing I wanted to see by setting up this PR and testing against terra main
was if we could pass without that list of exceptions because it would be hard for us to keep with special deprecation warnings in terra that should be allowed, but thankfully our tests pass without any exceptions.
test/base.py
Outdated
# Use testtools by default as a (mostly) drop in replacement for | ||
# unittest's TestCase. This will enable the fixtures used for capturing stdout | ||
# stderr, and pylogging to attach the output to stestr's result stream. | ||
USE_TESTTOOLS = os.environ.get("QE_USE_TESTOOLS", "TRUE") == "TRUE" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made testtools a required import. It is a dependency of fixtures
any way. Still, I personally had a niche use case for the tests not deriving from testtools.TestCase
so I left that a possibility. If it becomes a problem we can remove it. My niche case is that for some things I like to use pytest plugins for debugging. pytest can run unittest
tests but it can not run testtools
tests.
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.
This should be ready now. I reworked it some after further investigation and updated the pull request description. It could be backported but it's not really needed until qiskit 1.0. Maybe we will not be supporting the 0.5 branch by then any way? |
test/base.py
Outdated
def assertExperimentDone( | ||
self, | ||
experiment_data: ExperimentData, | ||
timeout: float = 120, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this could be tied to TEST_TIMEOUT
? At least it doesn't make sense for the default to be 120 if TEST_TIMEOUT
is 60.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, tying this to TEST_TIMEOUT
seems kind of reasonable. In practice that will never be reached unless there is some timing uncertainty in the signal.alarm
callback, but it would work for the case when not using testtools. Another option might be to get rid of this timeout and just rely on testtools time out. I think this may have been an earlier attempt to do what the time out fixture is doing now. What do you think?
Also, this time out is not really a change in this PR -- this is existing code that just got shifted down in scope.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I only noticed it when reviewing this PR (and I also just noticed a few cases in tests where a large timeout value is being manually passed, which is also effectively not doing anything). I think the parameter could be worth keeping in case someone wants a timeout while not using testtools, and setting it to TEST_TIMEOUT
may be a bit easier to use. I'm not very hung up over it either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this looks good to me overall. I didn't realize the testtools switch wasn't working. Should QE_USE_TESTTOOLS
be added to passenv
in tox.ini
so users can use the option with tox? Though I'm not sure why users would turn it off (to disable the timeout maybe?). Maybe this option can be mentioned in the contributor guide too.
I referenced this in my inline comment here. Personally, I have a niche use case for not using testtools. I wasn't sure if it was of wide enough interest to document, but I wanted to keep it possible if it was not too much burden. My particular use case is that sometimes I prefer to run the tests with I could add You can disable the time out by setting
I think for this PR I will:
Let me know if you think I should do something different. |
@wshanks fwiw, compatibility with pytest is one of the more frequently asked for features in stestr. I have a wip pr up adding it here: mtreinish/stestr#355 . Longer term we might want to look at using pytest for test suites because I'm not really happy with the state of the testtools stack either. |
@wshanks Right, I should have clarified I meant specifically what new users following the contributor guide might want to do with |
@mtreinish That is good to know about. So that would allow writing pytest-style tests and executing them with stestr. The main use case I had was running tests with |
Yeah, the stestr PR enables using stestr to run pytest-style tests. There isn't an alternative with stestr to support what The issue with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good besides one minor thing.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also needs to be added to passenv
, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, the text says that we are setting it, so I added it to setenv
. This matches what is in the Qiskit tox.ini. We have gone the whole time without it. Funny, I haven't really noticed the difference enough to be conscious of it. Maybe the experiments test runs are a little spammier than the Qiskit ones...
@mtreinish Since I just ran into it again, I will mention it here. |
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).
This change removes the dependence on
QiskitTestCase
, replacing itwith a direct dependence on
unittest.TestCase
andtesttools.TestCase
.As with
QiskitTestCase
, the ability to run the tests based either onunittest.TestCase
ortesttools.TestCase
(aunittest.TestCase
subclass) is preserved. For qiskit-experiments, the ability is actually
restored because the timeout feature added in
#1246
had introduced a hard dependence on
testtools
.Specific changes:
testtools
andfixtures
torequirements-dev.txt
as requiredtest dependencies.
QE_USE_TESTTOOLS
environment variable to control whether testsare based on
testtools.TestCase
rather than checking iftesttools
is installed.
QiskitTestCase
used extra code to ensure that
setUp
and other test class methodsalways called their parents and that those methods are not called from
individual tests.
testtools.TestCase
does these checks as well. Sinceqiskit-experiments always uses
testtools
in CI, it can rely ontesttools
for these checks and just not do them for the alternateunittest
execution.QiskitExperimentsTestCase
from acreate_base_test_case
function. This function allows the base test class to be generated
based on either
testtools.TestCase
orunittest.TestCase
so that theunittest
variant can be tested for regressions even when thetesttools
variant is enabled.Closes #1282.