-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Pytest and translations_test.py (#309)
Closes #305. ## Both Pytest and Jest tests We now have tests both in Python and JavaScript. That is because this is a multilingual repository, and we can only test code in the language it's written in. The tests are organized into `tests/{js,py}`. ## Changes `tox -e py` and adds `tox -e docs` Before, `tox -e py` was used to build example_docs. But that is different than every single Qiskit project, where `tox -e py` normally runs tests and you use `tox -e docs` to build the docs. This PR aligns us with the greater Qiskit ecosystem. ## Uses Pytest rather than unittest Most Qiskit projects use unittest/stestr (a way to parallelize unittest), although a few have switched to Pytest: Yeah, there are a few projects using Pytest but indeed most use unittest/stestr ``` ❯ rg pytest -g '*.txt' -l qiskit-ionq/requirements-test.txt mthree/requirements-dev.txt qiskit-qasm3-import/requirements-dev.txt qiskit-metal/requirements-dev.txt ``` If I remember correctly from when I first joined the project, Jake (or Matthew) was interested in eventually migrating more of Qiskit to Pytest and that they don't love stestr/unittest. But it's a bigger migration to do that I've found that Pytest in general is more ergonomic & also more flexible than unittest. For example, it has neat support for parametrization to make it easy to write several similar tests. And it's easier for developers to use because you only ever need to use `assert <some condition>`, rather than figuring out which to use between `self.assertEqual` vs `self.assertTrue` vs `self.assertContains` etc. Pytest will make the output of the `assert` useful for you automatically.
- Loading branch information
1 parent
6cbbe18
commit 20eda07
Showing
10 changed files
with
114 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
[build-system] | ||
requires = ["setuptools"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
[tool.pytest.ini_options] | ||
addopts = [ | ||
"--import-mode=importlib", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ matplotlib | |
sphinx-design | ||
sphinx-autodoc-typehints | ||
nbsphinx==0.9.* | ||
pytest==7.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import pytest | ||
|
||
from qiskit_sphinx_theme.translations import get_language_label, get_translation_url | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"docs_url_prefix,page,expected", | ||
[ | ||
("", "index", "/index.html"), | ||
("", "subdir/my_page", "/subdir/my_page.html"), | ||
("ecosystem/finance", "index", "/ecosystem/finance/index.html"), | ||
("ecosystem/finance", "subdir/my_page", "/ecosystem/finance/subdir/my_page.html"), | ||
] | ||
) | ||
def test_get_translation_url_default_language( | ||
docs_url_prefix: str, page: str, expected: str | ||
) -> None: | ||
"""For the default language (English), we leave off /locale from the URL.""" | ||
assert get_translation_url( | ||
content_prefix_option=docs_url_prefix, code="en", pagename=page | ||
) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"docs_url_prefix,page,expected", | ||
[ | ||
("", "index", "/locale/fr_FR/index.html"), | ||
("", "subdir/my_page", "/locale/fr_FR/subdir/my_page.html"), | ||
("ecosystem/finance", "index", "/ecosystem/finance/locale/fr_FR/index.html"), | ||
( | ||
"ecosystem/finance", | ||
"subdir/my_page", | ||
"/ecosystem/finance/locale/fr_FR/subdir/my_page.html", | ||
), | ||
] | ||
) | ||
def test_get_translation_url_translated_language( | ||
docs_url_prefix: str, page: str, expected: str | ||
) -> None: | ||
"""For translations, the URL should include /locale/<code>/.""" | ||
assert get_translation_url( | ||
content_prefix_option=docs_url_prefix, code="fr_FR", pagename=page | ||
) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"language_code,expected", | ||
[ | ||
("bn_BN", "Bengali"), | ||
("fr_FR", "French"), | ||
("en", "English"), | ||
# If the language code cannot be found, we set the label to that code. | ||
("unknown_code", "unknown_code"), | ||
] | ||
) | ||
def test_get_language_label(language_code: str, expected: str) -> None: | ||
"""Look up the label corresponding to the language_code.""" | ||
translations_list = [ | ||
('en', 'English'), | ||
('bn_BN', 'Bengali'), | ||
('fr_FR', 'French'), | ||
] | ||
assert get_language_label(language_code, translations_list) == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters