From b3ad3b4f605c39319de53ab227eb53b0318d5a78 Mon Sep 17 00:00:00 2001 From: Rebecca Graber Date: Mon, 18 Dec 2023 11:05:56 -0500 Subject: [PATCH] fix: add unit test to xblock so there is a coverage file to upload (#426) --- CHANGELOG.rst | 2 ++ .../requirements/test.in | 1 + .../test_{{cookiecutter.package_name}}.py | 21 +++++++++++++++++++ .../{{cookiecutter.repo_name}}/tox.ini | 5 ++++- 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 cookiecutter-xblock/{{cookiecutter.repo_name}}/tests/test_{{cookiecutter.package_name}}.py diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c1af2393..ad3ab370 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -18,6 +18,8 @@ Changed Changed ======= - Remove incorrect build step from xblock ci template +- Fix docstrings +- Add a unit test so coverage can run 2023-12-13 ********** diff --git a/cookiecutter-xblock/{{cookiecutter.repo_name}}/requirements/test.in b/cookiecutter-xblock/{{cookiecutter.repo_name}}/requirements/test.in index 6797160b..3a923b40 100644 --- a/cookiecutter-xblock/{{cookiecutter.repo_name}}/requirements/test.in +++ b/cookiecutter-xblock/{{cookiecutter.repo_name}}/requirements/test.in @@ -6,3 +6,4 @@ pytest-cov # pytest extension for code coverage statistics pytest-django # pytest extension for better Django support code-annotations # provides commands used by the pii_check make target. +xblock-sdk # provides workbench settings for testing diff --git a/cookiecutter-xblock/{{cookiecutter.repo_name}}/tests/test_{{cookiecutter.package_name}}.py b/cookiecutter-xblock/{{cookiecutter.repo_name}}/tests/test_{{cookiecutter.package_name}}.py new file mode 100644 index 00000000..c98a3e74 --- /dev/null +++ b/cookiecutter-xblock/{{cookiecutter.repo_name}}/tests/test_{{cookiecutter.package_name}}.py @@ -0,0 +1,21 @@ +""" +Tests for {{cookiecutter.class_name}} +""" + +from django.test import TestCase +from xblock.fields import ScopeIds +from xblock.test.toy_runtime import ToyRuntime + +from my_xblock import MyXBlock + + +class Test{{cookiecutter.class_name}}(TestCase): + """Tests for {{cookiecutter.class_name}}""" + def test_my_student_view(self): + """Test the basic view loads.""" + scope_ids = ScopeIds('1', '2', '3', '4') + block = MyXBlock(ToyRuntime(), scope_ids=scope_ids) + frag = block.student_view() + as_dict = frag.to_dict() + content = as_dict['content'] + self.assertIn('MyXBlock: count is now', content, 'XBlock did not render correct student view') diff --git a/cookiecutter-xblock/{{cookiecutter.repo_name}}/tox.ini b/cookiecutter-xblock/{{cookiecutter.repo_name}}/tox.ini index d62fb3bc..bf3ae14a 100644 --- a/cookiecutter-xblock/{{cookiecutter.repo_name}}/tox.ini +++ b/cookiecutter-xblock/{{cookiecutter.repo_name}}/tox.ini @@ -31,7 +31,7 @@ ignore = D101,D200,D203,D212,D215,D404,D405,D406,D407,D408,D409,D410,D411,D412,D match-dir = (?!migrations) [pytest] -DJANGO_SETTINGS_MODULE = translation_settings +DJANGO_SETTINGS_MODULE = workbench.settings addopts = --cov {{ cookiecutter.package_name }} --cov-report term-missing --cov-report xml norecursedirs = .* docs requirements site-packages @@ -40,7 +40,10 @@ deps = django32: Django>=3.2,<4.0 django40: Django>=4.0,<4.1 -r{toxinidir}/requirements/test.txt +allowlist_externals = + mkdir commands = + mkdir -p var pytest {posargs} [testenv:docs]