diff --git a/CHANGELOG.md b/CHANGELOG.md index c129885..174cc29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # pygrader +## 2.1.0 + +- Add `complexity` check, which fails if any function exceeds a cognitive complexity threshold (via complexipy) +- Ship a `py.typed` marker so library users get type information; mypy no longer ignores missing imports globally +- Migrate from `os.path` to `pathlib` internally (public signatures still take `str`) +- Migrate the test suite from `unittest` to `pytest` + ## 2.0.0 - Desktop Pygrader can now grade multiple projects via globs diff --git a/CLAUDE.md b/CLAUDE.md index 651dc9a..d34c4a7 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -8,7 +8,7 @@ This document provides context and guidelines for Claude Sonnet 4.5 when working ### Key Information - **Language**: Python 3.10+ -- **Version**: 1.7.1 +- **Version**: 2.1.0 - **License**: GPL-3.0 - **Architecture**: Modular check-based system with CLI and web interfaces - **Current Branch**: `show-check-results` (default: `main`) @@ -163,8 +163,8 @@ The `Grader` class (`grader/grader.py`) orchestrates: - **Line Length**: Follow project conventions ### Testing -- **Framework**: unittest with coverage (not pytest) -- **Test Runner**: `uv run -m unittest` or `just test` +- **Framework**: pytest with coverage (plain `assert`, `pytest.raises`, `caplog`/`log_output` fixture; `unittest.mock` for mocking) +- **Test Runner**: `uv run pytest` or `just test` - **Unit Tests**: `tests/unit/test_*.py` - test individual components (~20 seconds for the whole suite) - **Functional Tests**: `tests/functional/test_functional.py` - end-to-end tests - **Coverage Target**: 85% minimum (enforced by `just coverage`) @@ -190,10 +190,10 @@ The `Grader` class (`grader/grader.py`) orchestrates: - **Run Specific Tests**: ```bash # Run specific test class - uv run python -m unittest tests.unit.test_pylint_check.TestPylintCheck -v + uv run pytest tests/unit/test_pylint_check.py::TestPylintCheck -v # Run specific test method - uv run python -m unittest tests.unit.test_pylint_check.TestPylintCheck.test_01_pylint_called -v + uv run pytest tests/unit/test_pylint_check.py::TestPylintCheck::test_01_pylint_called -v ``` ### Adding New Checks @@ -261,8 +261,8 @@ just unit_tests # Unit tests only just functional_tests # Functional tests only # Manual test execution -uv run -m unittest discover -s tests/unit -p "test_*.py" -v -uv run -m unittest discover -s tests/functional -p "test_*.py" +uv run pytest tests/unit -v +uv run pytest tests/functional ``` #### Coverage Analysis @@ -270,7 +270,7 @@ uv run -m unittest discover -s tests/functional -p "test_*.py" just coverage # Run tests with coverage (85% minimum) # Manual coverage -uv run coverage run --source=grader,desktop -m unittest discover -s tests/unit -p "test_*.py" +uv run coverage run --source=grader,desktop -m pytest tests/unit uv run coverage report -m --fail-under 85 --sort=cover uv run coverage lcov -o lcov.info # Generate lcov report ``` diff --git a/config/complexity.json b/config/complexity.json new file mode 100644 index 0000000..9099d9e --- /dev/null +++ b/config/complexity.json @@ -0,0 +1,9 @@ +{ + "checks": [ + { + "name": "complexity", + "is_venv_required": false, + "max_complexity": 15 + } + ] +} diff --git a/desktop/main.py b/desktop/main.py index 454756b..dc1793f 100644 --- a/desktop/main.py +++ b/desktop/main.py @@ -5,7 +5,6 @@ """ import glob -import os import shutil import zipfile from pathlib import Path @@ -139,7 +138,7 @@ def run_grader() -> int: # TODO - Add output to a file print(reporter.to_string()) - if os.path.exists(const.WORK_DIR): + if const.WORK_DIR.exists(): shutil.rmtree(const.WORK_DIR) return 1 if has_failure else 0 diff --git a/docs/source/config.rst b/docs/source/config.rst index 84e011e..4347a10 100644 --- a/docs/source/config.rst +++ b/docs/source/config.rst @@ -5,22 +5,23 @@ The grader supports configuration files in JSON format. These configuration files define the checks to be performed on student submissions, along with their parameters and scoring criteria. +The file passed via ``--config`` can be a local path, an ``http``/``https``/``ftp`` URL, +or a ``cove://`` URI pointing to a JSON item (requires the ``COVE_API_KEY`` environment variable). + Configuration Schema -------------------- -Root Structure -~~~~~~~~~~~~~~ - -The configuration file has two possible root-level structures: +There are two kinds of JSON files: -1. **Check-based configuration** - The most common format with a ``checks`` array -2. **Project structure configuration** - For defining required file patterns +1. **Grading configuration** - The file passed via ``--config``, with a ``checks`` array +2. **Project structure configuration** - A separate file referenced by the ``structure`` check -Check-Based Configuration -~~~~~~~~~~~~~~~~~~~~~~~~~ +Grading Configuration +~~~~~~~~~~~~~~~~~~~~~ The root object must contain a key called ``checks`` with an array of check objects. -Optionally, it can also include an ``environment`` object for global environment variables. +Optionally, it can also include ``environment`` and ``venv`` objects. +Unknown root-level keys are silently ignored, so double-check spelling. .. code-block:: json @@ -59,6 +60,9 @@ Root-Level Properties - ``name``: Custom name for the virtual environment directory, to avoid conflicts with a student's own ``.venv``. Default: ``.venv-pygrader``. + Any other key inside ``venv`` makes the run fail. The venv is only created if at least one + check has ``is_venv_required`` set to ``true``. + Example: .. code-block:: json @@ -83,7 +87,8 @@ Check Object Properties Common Properties ^^^^^^^^^^^^^^^^^ -All check types support these common properties: +All check types support these common properties. Any key not listed here or under the +check's own section is rejected and makes the run fail. ``name`` (required) The name/type of the check to perform. Supported values: @@ -95,15 +100,17 @@ All check types support these common properties: - ``tests`` - Runs unit tests - ``structure`` - Validates project file structure - ``spec`` - Grades the project against a markdown specification using an LLM + - ``complexity`` - Fails if any function exceeds a cognitive complexity threshold (via complexipy) -``max_points`` (optional) +``max_points`` (required for all checks except ``structure`` and ``complexity``) The maximum score that can be awarded for this check. Can be an integer or float value. - Some checks (like ``structure``) may not require this field. + The ``structure`` and ``complexity`` checks are pass/fail and do not accept this field. ``is_venv_required`` (required) Boolean indicating if the check should be run within a virtual environment. - Can be a boolean value or a string ``"false"`` or ``"true"``. + Must be a JSON boolean (``true``/``false``). Strings are not parsed - ``"false"`` + is a non-empty string and is treated as ``true``. ``environment`` (optional) Check-specific environment configuration that overrides global environment variables. @@ -156,9 +163,11 @@ Pylint Check """""""""""" ``pylintrc_path`` (optional) - Path to a custom pylint configuration file. + Local path to a custom pylint configuration file. Supports template variables like ``${{config_dir}}``. - + URLs and Cove URIs are not supported. Default: the bundled ``config/2024.pylintrc``. + If the file does not exist, pylint runs with its own defaults. + Example: .. code-block:: json @@ -173,12 +182,14 @@ Pylint Check Tests Check """"""""""" -``tests_path`` (optional) - Array of paths or URLs to test files to run. - Can be local file paths or URLs to remote test files. +``tests_path`` (required) + Array of test files to run. + Each entry can be a local path, a URL, or a ``cove://`` URI pointing to a Python item + (requires the ``COVE_API_KEY`` environment variable). ``default_test_score`` (optional) Default score assigned to each test if not specified in ``test_score_mapping``. + Default: ``0``. ``test_score_mapping`` (optional) Object mapping test class or test function names to their point values. @@ -208,10 +219,13 @@ Structure Check """"""""""""""" ``is_fatal`` (optional) - Boolean indicating whether a failing structure check should abort the rest of the - grading run. Default: ``false``. + Boolean marking the check as fatal. Default: ``false``. + + .. note:: + The flag is stored on the check but not acted upon yet - a failing structure check + does not currently abort the grading run. -``structure_file`` (optional) +``structure_file`` (required) Path, URL or Cove URI to a JSON file defining the required project structure. Supports template variables like ``${{config_dir}}``. Can be a local path, a URL to a remote structure definition, or a ``cove://`` URI @@ -237,6 +251,33 @@ Example with a Cove URI: "structure_file": "cove://fmi-python/project-structure" } +Complexity Check +"""""""""""""""" + +Runs complexipy on the project's source files (tests are excluded). The check passes when no +function is more complex than the threshold, and lists the offending functions otherwise. + +``max_complexity`` (optional) + The highest cognitive complexity a single function may have. Default: ``15``. + +``is_fatal`` (optional) + Boolean marking the check as fatal. Default: ``false``. Not acted upon yet. + +Example: + +.. code-block:: json + + { + "name": "complexity", + "is_venv_required": false, + "max_complexity": 15 + } + +Type Hints and Coverage Checks +"""""""""""""""""""""""""""""" + +The ``type-hints`` and ``coverage`` checks take no options beyond the common properties. + Spec Check """""""""" @@ -350,6 +391,9 @@ Configuration files support template variables for dynamic path resolution: ``${{config_dir}}`` Expands to the directory containing the configuration file. Useful for referencing other config files relative to the main config. + For remote and Cove configs, this is the temporary directory the config was downloaded to. + +Any other ``${{...}}`` placeholder in the file is an error. Example: diff --git a/grader/checks/checks_factory.py b/grader/checks/checks_factory.py index e1d4c8a..bb4057f 100644 --- a/grader/checks/checks_factory.py +++ b/grader/checks/checks_factory.py @@ -1,6 +1,7 @@ """Factory for creating the checks objects.""" from grader.checks.abstract_check import AbstractCheck +from grader.checks.complexity_check import ComplexityCheck from grader.checks.coverage_check import CoverageCheck from grader.checks.pylint_check import PylintCheck from grader.checks.requirements_check import RequirementsCheck @@ -19,6 +20,7 @@ "structure": StructureCheck, "tests": RunTestsCheck, "spec": SpecCheck, + "complexity": ComplexityCheck, } diff --git a/grader/checks/complexity_check.py b/grader/checks/complexity_check.py new file mode 100644 index 0000000..e2306e0 --- /dev/null +++ b/grader/checks/complexity_check.py @@ -0,0 +1,92 @@ +""" +Module containing the complexity check. + +It uses the complexipy python library directly to measure the cognitive complexity of each function. +""" + +import logging + +import complexipy + +from grader.checks.abstract_check import NonScoredCheck +from grader.exceptions import CheckError +from grader.models.check_result import NonScoredCheckResult +from grader.utils import files + +logger = logging.getLogger("grader") + +DEFAULT_MAX_COMPLEXITY = 15 + + +class ComplexityCheck(NonScoredCheck): + """The Complexity check class.""" + + def __init__( + self, + name: str, + project_root: str, + max_complexity: int = DEFAULT_MAX_COMPLEXITY, + is_fatal: bool = False, + is_venv_required: bool = False, + env_vars: dict[str, str] | None = None, + assets: list[str] | None = None, + ): + """ + Initialize the complexity check. + + :param name: The name of the check. + :param project_root: The root directory of the project. + :param max_complexity: The highest cognitive complexity a single function may have. + :param is_fatal: Whether the check is fatal. + :param is_venv_required: Whether a virtual environment is required. + :param env_vars: Optional environment variables for the check. + :param assets: Optional list of resource sources (paths, URLs or Cove URIs) for the check. + """ + super().__init__(name, project_root, is_fatal, is_venv_required, env_vars, assets) + self.__max_complexity = max_complexity + + def run(self) -> NonScoredCheckResult: + """ + Run complexipy on all source files of the project. + + The check passes when no function is more complex than the configured maximum. + + Files that cannot be parsed fail the check but do not stop the analysis of the other files. + + :raises CheckError: If the source files cannot be found. + :returns: The result of the check, listing the offending functions in the info field. + :rtype: NonScoredCheckResult + """ + self._pre_run() + + try: + source_files = files.find_all_source_files(self._project_root) + except OSError as error: + logger.error("Error while finding python files: %s", error) + raise CheckError("Error while finding python files") from error + + offenders: list[str] = [] + unparsable: list[str] = [] + for source_file in source_files: + try: + file_complexity = complexipy.file_complexity(source_file) + except ValueError as error: # complexipy raises ValueError for unreadable or invalid files + logger.warning("Could not analyse %s: %s", source_file, error) + unparsable.append(source_file) + continue + + offenders.extend( + f"{source_file}:{function.line_start} {function.name} ({function.complexity})" + for function in file_complexity.functions + if function.complexity > self.__max_complexity + ) + + if offenders or unparsable: + info = [] + if offenders: + info.append(f"Functions above complexity {self.__max_complexity}: " + ", ".join(offenders)) + if unparsable: + info.append("Could not analyse: " + ", ".join(unparsable)) + return NonScoredCheckResult(self.name, False, ". ".join(info), "") + + return NonScoredCheckResult(self.name, True, f"All functions are within complexity {self.__max_complexity}", "") diff --git a/grader/checks/coverage_check.py b/grader/checks/coverage_check.py index d94c006..2d37dae 100644 --- a/grader/checks/coverage_check.py +++ b/grader/checks/coverage_check.py @@ -43,7 +43,7 @@ def __init__( """ super().__init__(name, max_points, project_root, is_venv_required, env_vars, assets) - self.__coverage_full_path = COVERAGE_PATH + self.__coverage_full_path = str(COVERAGE_PATH) def run(self) -> ScoredCheckResult: """ diff --git a/grader/checks/pylint_check.py b/grader/checks/pylint_check.py index 3f06b77..ba1a733 100644 --- a/grader/checks/pylint_check.py +++ b/grader/checks/pylint_check.py @@ -5,9 +5,9 @@ import itertools import logging -import os import re from io import StringIO +from pathlib import Path from pylint.reporters.text import TextReporter @@ -72,10 +72,10 @@ def run(self) -> ScoredCheckResult: pylint_args.append("--fail-under=0") pylint_args.append("--extension-pkg-whitelist=pygame") - if os.path.exists(self.__pylintrc_path): + if Path(self.__pylintrc_path).exists(): pylint_args.extend(["--rcfile", self.__pylintrc_path]) - command = [const.PYLINT_PATH] + pylint_args # Current working directory is set in the process.run method + command = [str(const.PYLINT_PATH)] + pylint_args # Current working directory is set in the process.run method try: results = process.run(command, current_directory=self._project_root, env_vars=self.env_vars) except (OSError, ValueError) as error: diff --git a/grader/checks/requirements_check.py b/grader/checks/requirements_check.py index 380bff8..e0a3875 100644 --- a/grader/checks/requirements_check.py +++ b/grader/checks/requirements_check.py @@ -5,7 +5,6 @@ """ import logging -import os from pathlib import Path from grader.checks.abstract_check import ScoredCheck @@ -43,8 +42,8 @@ def __init__( """ super().__init__(name, max_points, project_root, is_venv_required, env_vars, assets) - self.__requirements_path = os.path.join(self._project_root, REQUIREMENTS_FILENAME) - self.__pyproject_path = os.path.join(self._project_root, PYPROJECT_FILENAME) + self.__requirements_path = Path(self._project_root) / REQUIREMENTS_FILENAME + self.__pyproject_path = Path(self._project_root) / PYPROJECT_FILENAME self.__is_checking_install = is_checking_install def run(self) -> ScoredCheckResult: @@ -57,10 +56,7 @@ def run(self) -> ScoredCheckResult: """ self._pre_run() - requirements = Path(self.__requirements_path) - pyproject = Path(self.__pyproject_path) - - files_to_search = [requirements, pyproject] + files_to_search = [self.__requirements_path, self.__pyproject_path] is_one_of_files_present = any(file_path.exists() for file_path in files_to_search) diff --git a/grader/checks/run_tests_check.py b/grader/checks/run_tests_check.py index fa8c90e..ec162bb 100644 --- a/grader/checks/run_tests_check.py +++ b/grader/checks/run_tests_check.py @@ -1,9 +1,9 @@ """Module containing the check for running tests against the submitted code.""" import logging -import os from collections import defaultdict from dataclasses import dataclass +from pathlib import Path from grader.checks.abstract_check import ScoredCheck from grader.exceptions import CheckError, ResourceError @@ -117,11 +117,9 @@ def __pytest_run(self) -> str: :rtype: str :raises CheckError: If pytest fails to execute or encounters an error. """ - if os.path.isabs(self._project_root): - pytest_root_dir = PYTEST_ROOT_DIR_ARG.format(self._project_root) - else: - pytest_root_dir = PYTEST_ROOT_DIR_ARG.format(os.path.join(os.getcwd(), self._project_root)) - command = [PYTEST_PATH] + PYTEST_ARGS + [pytest_root_dir] + self.__tests_path + # Joining with an absolute project root yields the root itself + pytest_root_dir = PYTEST_ROOT_DIR_ARG.format(Path.cwd() / self._project_root) + command = [str(PYTEST_PATH)] + PYTEST_ARGS + [pytest_root_dir] + self.__tests_path pythonpath_env = process.extend_env_variable("PYTHONPATH", self._project_root) diff --git a/grader/checks/spec_check.py b/grader/checks/spec_check.py index c9250aa..376ebea 100644 --- a/grader/checks/spec_check.py +++ b/grader/checks/spec_check.py @@ -8,6 +8,7 @@ import json import logging import os +from pathlib import Path import requests from dotenv import load_dotenv @@ -111,7 +112,12 @@ def __collect_source(self) -> str: """ chunks = [] for path in find_all_source_files(self._project_root): - relative_path = os.path.relpath(path, self._project_root) + resolved_path = Path(path).resolve() + project_root = Path(self._project_root).resolve() + # Symlinked files can resolve to a location outside the project root + relative_path = ( + resolved_path.relative_to(project_root) if resolved_path.is_relative_to(project_root) else resolved_path + ) try: with open(path, "r", encoding="utf-8") as file_pointer: chunks.append(f"# === {relative_path} ===\n{file_pointer.read()}") diff --git a/grader/checks/type_hints_check.py b/grader/checks/type_hints_check.py index b2af7d3..09e3b4f 100644 --- a/grader/checks/type_hints_check.py +++ b/grader/checks/type_hints_check.py @@ -40,8 +40,8 @@ def __init__( """ super().__init__(name, max_points, project_root, is_venv_required, env_vars, assets) - self.__mypy_binary = MYPY_PATH - self.__mypy_arguments = ["--config-file", MYPY_TYPE_HINT_CONFIG, "--linecount-report", REPORTS_TEMP_DIR] + self.__mypy_binary = str(MYPY_PATH) + self.__mypy_arguments = ["--config-file", MYPY_TYPE_HINT_CONFIG, "--linecount-report", str(REPORTS_TEMP_DIR)] self.__mypy_max_score = 1 def run(self) -> ScoredCheckResult: diff --git a/grader/grader.py b/grader/grader.py index 9f3e84b..b6be0d4 100644 --- a/grader/grader.py +++ b/grader/grader.py @@ -3,6 +3,7 @@ import os import shutil from logging import Logger +from pathlib import Path import grader.utils.constants as const from grader.checks.abstract_check import AbstractCheck, NonScoredCheck, ScoredCheck @@ -78,7 +79,7 @@ def grade(self, project_root: str, run_id: str) -> GradingResult: self.__logger.info("Running checks for student %s", run_id) self.__logger.debug("Project root: %s", project_root) - if not os.path.exists(project_root): + if not Path(project_root).exists(): self.__logger.error("Project root directory does not exist") raise InvalidProjectRootError("Project root directory does not exist") @@ -158,7 +159,5 @@ def __cleanup(self, project_root: str) -> None: """ shutil.rmtree(const.TEMP_FILES_DIR, ignore_errors=True) - coverage_file_full_path = os.path.join(project_root, const.COVERAGE_FILE) - if os.path.exists(coverage_file_full_path): - os.remove(coverage_file_full_path) - shutil.rmtree(os.path.join(project_root, const.PYTEST_CACHE), ignore_errors=True) + (Path(project_root) / const.COVERAGE_FILE).unlink(missing_ok=True) + shutil.rmtree(Path(project_root) / const.PYTEST_CACHE, ignore_errors=True) diff --git a/grader/py.typed b/grader/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/grader/utils/constants.py b/grader/utils/constants.py index 9264369..cc803a6 100644 --- a/grader/utils/constants.py +++ b/grader/utils/constants.py @@ -3,17 +3,18 @@ import os from importlib.metadata import version from importlib.resources import files +from pathlib import Path VERSION = version("pygrader") # Directories -ROOT_DIR = os.path.dirname(os.path.realpath(__name__)) -REPORTS_TEMP_DIR = os.path.join(ROOT_DIR, "reports") -CONFIG_DIR = os.path.join(ROOT_DIR, "config") -TEMP_FILES_DIR = os.path.join(ROOT_DIR, "temp_files") # TODO - Change this to be under WORK_DIR ? +ROOT_DIR = Path.cwd().resolve() +REPORTS_TEMP_DIR = ROOT_DIR / "reports" +CONFIG_DIR = ROOT_DIR / "config" +TEMP_FILES_DIR = ROOT_DIR / "temp_files" # TODO - Change this to be under WORK_DIR ? -WORK_DIR = os.path.join("/tmp", "pygrader") +WORK_DIR = Path("/tmp") / "pygrader" # Python PYTHON_BIN_WINDOWS = "python.exe" @@ -27,8 +28,8 @@ PYPROJECT_FILENAME = "pyproject.toml" VENV_NAME = ".venv-pygrader" POSSIBLE_VENV_DIRS = ["venv", ".venv", ".venv-pygrader"] -PIP_PATH_WINDOWS = os.path.join("Scripts", "pip.exe") -PIP_PATH_UNIX = os.path.join("bin", "pip") +PIP_PATH_WINDOWS = Path("Scripts", "pip.exe") +PIP_PATH_UNIX = Path("bin", "pip") PIP_PATH = PIP_PATH_WINDOWS if os.name == "nt" else PIP_PATH_UNIX @@ -40,17 +41,17 @@ MYPY_BIN = MYPY_BIN_WINDOWS if os.name == "nt" else MYPY_BIN_UNIX MYPY_TYPE_HINT_CONFIG = str(files("config").joinpath("mypy_type_hints_2024.ini")) -MYPY_LINE_COUNT_REPORT = os.path.join(REPORTS_TEMP_DIR, "linecount.txt") -MYPY_PATH_WINDOWS = os.path.join(VENV_NAME, "Scripts", MYPY_BIN) -MYPY_PATH_UNIX = os.path.join(VENV_NAME, "bin", MYPY_BIN) +MYPY_LINE_COUNT_REPORT = REPORTS_TEMP_DIR / "linecount.txt" +MYPY_PATH_WINDOWS = Path(VENV_NAME, "Scripts", MYPY_BIN) +MYPY_PATH_UNIX = Path(VENV_NAME, "bin", MYPY_BIN) MYPY_PATH = MYPY_PATH_WINDOWS if os.name == "nt" else MYPY_PATH_UNIX # Pylint constants -PYLINT_BIN_WINDOWS = os.path.join("Scripts", "pylint.exe") -PYLINT_BIN_UNIX = os.path.join("bin", "pylint") +PYLINT_BIN_WINDOWS = Path("Scripts", "pylint.exe") +PYLINT_BIN_UNIX = Path("bin", "pylint") PYLINT_BIN = PYLINT_BIN_WINDOWS if os.name == "nt" else PYLINT_BIN_UNIX -PYLINT_PATH = os.path.join(VENV_NAME, PYLINT_BIN) +PYLINT_PATH = Path(VENV_NAME, PYLINT_BIN) PYLINTRC = str(files("config").joinpath("2024.pylintrc")) # Pytest constants @@ -58,8 +59,8 @@ PYTEST_BIN_UNIX = "pytest" PYTEST_BIN = PYTEST_BIN_WINDOWS if os.name == "nt" else PYTEST_BIN_UNIX -PYTEST_PATH_WINDOWS = os.path.join(VENV_NAME, "Scripts", PYTEST_BIN) -PYTEST_PATH_UNIX = os.path.join(VENV_NAME, "bin", PYTEST_BIN) +PYTEST_PATH_WINDOWS = Path(VENV_NAME, "Scripts", PYTEST_BIN) +PYTEST_PATH_UNIX = Path(VENV_NAME, "bin", PYTEST_BIN) PYTEST_PATH = PYTEST_PATH_WINDOWS if os.name == "nt" else PYTEST_PATH_UNIX PYTEST_ARGS = ["--no-header", "-r A"] PYTEST_ROOT_DIR_ARG = "--rootdir={}" @@ -71,8 +72,8 @@ COVERAGE_BIN_UNIX = "coverage" COVERAGE_BIN = COVERAGE_BIN_WINDOWS if os.name == "nt" else COVERAGE_BIN_UNIX -COVERAGE_PATH_WINDOWS = os.path.join(VENV_NAME, "Scripts", COVERAGE_BIN) -COVERAGE_PATH_UNIX = os.path.join(VENV_NAME, "bin", COVERAGE_BIN) +COVERAGE_PATH_WINDOWS = Path(VENV_NAME, "Scripts", COVERAGE_BIN) +COVERAGE_PATH_UNIX = Path(VENV_NAME, "bin", COVERAGE_BIN) COVERAGE_PATH = COVERAGE_PATH_WINDOWS if os.name == "nt" else COVERAGE_PATH_UNIX COVERAGE_RUN_ARGS = ["run", "-m"] COVERAGE_RUN_PYTEST_ARGS = ["pytest"] @@ -92,6 +93,6 @@ ".pytest_cache", "_MACOSX", "__MACOSX", - os.path.join("build", "lib"), + str(Path("build", "lib")), *POSSIBLE_VENV_DIRS, ] diff --git a/grader/utils/external_resources.py b/grader/utils/external_resources.py index 2c0245e..5ed5395 100644 --- a/grader/utils/external_resources.py +++ b/grader/utils/external_resources.py @@ -62,13 +62,11 @@ def to_file(self) -> str: return self._source content = self.read() - file_path = os.path.join(TEMP_FILES_DIR, self._filename) - os.makedirs(TEMP_FILES_DIR, exist_ok=True) + file_path = TEMP_FILES_DIR / self._filename + TEMP_FILES_DIR.mkdir(parents=True, exist_ok=True) + file_path.write_text(content, encoding="utf-8") - with open(file_path, "w+", encoding="utf-8") as file: - file.write(content) - - return file_path + return str(file_path) @staticmethod def _sniff_resource_type(source: str) -> ResourceType: @@ -127,7 +125,7 @@ def _download_file_from_url(url: str) -> tuple[str, str]: except requests.RequestException as exc: raise ResourceError(f"Error downloading file from {url}") from exc - filename = os.path.basename(urlparse(url).path) or "downloaded_file" + filename = Path(urlparse(url).path).name or "downloaded_file" # URLs that require tokens (Github private repos) are out of scope for now return response.text, filename diff --git a/grader/utils/files.py b/grader/utils/files.py index 74ac878..3ff033f 100644 --- a/grader/utils/files.py +++ b/grader/utils/files.py @@ -1,6 +1,5 @@ """Module containing the file-related functions.""" -import os import zipfile from pathlib import Path @@ -56,9 +55,9 @@ def get_tests_directory_name(project_root_dir: str) -> str | None: :returns: The path to the tests directory if found, otherwise None """ for possible_directory in const.POSSIBLE_TEST_DIRS: - possible_tests_path = os.path.join(project_root_dir, possible_directory) - if os.path.exists(possible_tests_path): - return possible_tests_path + possible_tests_path = Path(project_root_dir) / possible_directory + if possible_tests_path.exists(): + return str(possible_tests_path) return None @@ -72,11 +71,7 @@ def find_all_files_under_directory(directory: str, extension: str) -> list[str]: :return: A list of all files under the directory with the specified extension :rtype: list[str] """ - files = [ - os.path.join(root, file) for root, _, files in os.walk(directory) for file in files if file.endswith(extension) - ] - - return files + return [str(file) for file in Path(directory).rglob(f"*{extension}") if file.is_file()] def is_path_zip(path: str) -> bool: @@ -102,7 +97,7 @@ def unzip_archive(archive_path: str, target_directory: str | None = None) -> str # Batch processing gives every submission's archive the same filename (e.g. "project.zip"), # so the parent directory name (unique per submission) is included to keep extractions apart. submission_dir = archive_path_obj.parent.name - working_directory = target_directory or os.path.join(const.WORK_DIR, submission_dir, archive_stem) + working_directory = target_directory or str(const.WORK_DIR / submission_dir / archive_stem) with zipfile.ZipFile(archive_path, "r") as zip_ref: zip_ref.extractall(working_directory) diff --git a/grader/utils/virtual_environment.py b/grader/utils/virtual_environment.py index fe5b1fd..b0f3f36 100644 --- a/grader/utils/virtual_environment.py +++ b/grader/utils/virtual_environment.py @@ -3,8 +3,8 @@ from __future__ import annotations # Python 3.14 will fix this import logging -import os import shutil +from pathlib import Path from typing import Self import grader.utils.constants as const @@ -41,7 +41,7 @@ def __init__( """ self._project_path = project_path # TODO - To fully allow for a custom venv name, we need to rethink how we handle paths in the constants - self._venv_path = os.path.join(project_path, name) + self._venv_path = Path(project_path) / name self.__is_keeping_venv_after_run = is_keeping_venv_after_run self.__is_keeping_existing_venv = is_keeping_existing_venv @@ -75,7 +75,7 @@ def setup(self) -> None: # Create new venv logger.log(VERBOSE, "Creating new venv") - create_venv_result = run([const.PYTHON_BIN, "-m", "venv", self._venv_path]) + create_venv_result = run([const.PYTHON_BIN, "-m", "venv", str(self._venv_path)]) if create_venv_result.returncode != 0: logger.error("Failed to create virtual environment") raise VirtualEnvironmentError("Failed to create virtual environment") @@ -83,15 +83,14 @@ def setup(self) -> None: # Install project as package # note: we haven't shown them `setup.py` so we shouldn't look for it? # only support `pyproject.toml` configuration for now - if os.path.exists(os.path.join(self._project_path, const.PYPROJECT_FILENAME)): + if (Path(self._project_path) / const.PYPROJECT_FILENAME).exists(): logger.log(VERBOSE, "Installing project as package") VirtualEnvironment.__install_project_as_package(self._venv_path, self._project_path) else: # if it is not a packaged project, check for requirements.txt and install them - requirements_path = os.path.join(self._project_path, const.REQUIREMENTS_FILENAME) + requirements_path = Path(self._project_path) / const.REQUIREMENTS_FILENAME - does_requirements_exist = os.path.exists(requirements_path) - if not does_requirements_exist: + if not requirements_path.exists(): logger.debug("No requirements.txt file found in the project directory") else: logger.log(VERBOSE, "Installing requirements") @@ -100,15 +99,15 @@ def setup(self) -> None: # Install grader dependencies logger.log(VERBOSE, "Installing grader dependencies") - grader_requirements_path = const.GRADER_REQUIREMENTS + grader_requirements_path = Path(const.GRADER_REQUIREMENTS) VirtualEnvironment.__install_requirements(self._venv_path, grader_requirements_path) def __remove_existing_venv(self) -> None: """Remove any existing virtual environment in the project directory.""" - possible_venv_paths = [os.path.join(self._project_path, venv_path) for venv_path in const.POSSIBLE_VENV_DIRS] + possible_venv_paths = [Path(self._project_path) / venv_path for venv_path in const.POSSIBLE_VENV_DIRS] for path in possible_venv_paths: - if os.path.exists(path): + if path.exists(): logger.log(VERBOSE, "Found existing venv at %s", path) shutil.rmtree(path) @@ -119,7 +118,7 @@ def teardown(self) -> None: shutil.rmtree(self._venv_path) @staticmethod - def __install_requirements(venv_path: str, requirements_path: str) -> None: + def __install_requirements(venv_path: Path, requirements_path: Path) -> None: """ Install the requirements specified in the requirements file into the virtual environment. @@ -131,16 +130,16 @@ def __install_requirements(venv_path: str, requirements_path: str) -> None: :return: None :rtype: None. """ - pip_path = os.path.join(venv_path, const.PIP_PATH) + pip_path = venv_path / const.PIP_PATH - output = run([pip_path, "install", "-r", requirements_path]) + output = run([str(pip_path), "install", "-r", str(requirements_path)]) if output.returncode != 0: logger.error("Failed to install requirements from %s", requirements_path) raise VirtualEnvironmentError(f"Failed to install requirements from {requirements_path}") @staticmethod - def __install_project_as_package(venv_path: str, project_path: str) -> None: + def __install_project_as_package(venv_path: Path, project_path: str) -> None: """ Install the project as a package into the virtual environment. @@ -152,10 +151,10 @@ def __install_project_as_package(venv_path: str, project_path: str) -> None: :return: None :rtype: None. """ - pip_path = os.path.join(venv_path, const.PIP_PATH) + pip_path = venv_path / const.PIP_PATH # Editable install fixes issues with directory structure renaming in pyproject.toml - output = run([pip_path, "install", "-e", project_path]) + output = run([str(pip_path), "install", "-e", project_path]) if output.returncode != 0: logger.error("Failed to install project from %s", project_path) diff --git a/justfile b/justfile index 8f11cec..8f934eb 100644 --- a/justfile +++ b/justfile @@ -10,20 +10,20 @@ init: lint: uv run ruff check {{project_content}} --fix uv run ruff format {{project_content}} - uv run mypy {{project_content}} --ignore-missing-imports + uv run mypy {{project_content}} uv run complexipy . # Tests test: unit_tests functional_tests unit_tests: - uv run -m unittest discover -s tests/unit -p "test_*.py" -v + uv run pytest tests/unit -v functional_tests: - uv run -m unittest discover -s tests/functional -p "test_*.py" + uv run pytest tests/functional coverage: - uv run coverage run --source={{packages}} -m unittest discover -s tests/unit -p "test_*.py" + uv run coverage run --source={{packages}} -m pytest tests/unit uv run coverage lcov -o lcov.info uv run coverage report -m --fail-under 85 --sort=cover diff --git a/mypy.ini b/mypy.ini index 62a1004..d461e5e 100644 --- a/mypy.ini +++ b/mypy.ini @@ -4,4 +4,6 @@ check_untyped_defs = True no_implicit_optional = True show_error_codes = True warn_unused_ignores = True -ignore_missing_imports = True \ No newline at end of file + +[mypy-cove_sdk.*] +ignore_missing_imports = True diff --git a/pyproject.toml b/pyproject.toml index c5d1e1e..81a985a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,10 +1,11 @@ [project] name = "pygrader" -version = "2.0.0" +version = "2.1.0" description = "Add your description here" readme = "README.md" requires-python = ">=3.12" dependencies = [ + "complexipy>=8.0.0", "dotenv>=0.9.9", "py-cove @ git+https://github.com/lyubolp/py-cove.git", "pylint>=4.0.8", @@ -23,8 +24,12 @@ build-backend = "setuptools.build_meta" packages = ["grader", "grader.checks", "grader.utils", "grader.models", "config"] [tool.setuptools.package-data] +grader = ["py.typed"] config = ["grader_requirements.txt", "mypy_type_hints_2024.ini", "2024.pylintrc"] +[tool.pytest.ini_options] +testpaths = ["tests"] + [tool.complexipy] paths = ["grader", "tests", "desktop", "web"] max-complexity-allowed = 15 @@ -40,7 +45,7 @@ py-cove = { git = "https://github.com/lyubolp/py-cove", rev = "v1.4.0" } [dependency-groups] dev = [ - "complexipy>=8.0.0", + "pytest>=9.1.1", "coverage>=7.16.0", "mypy>=2.3.1", "myst-parser>=5.1.0", diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..d898f4f --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,16 @@ +"""Shared pytest fixtures.""" + +from collections.abc import Callable + +import pytest + + +@pytest.fixture(name="log_output") +def fixture_log_output(caplog: pytest.LogCaptureFixture) -> Callable[[], list[str]]: + """ + Capture everything logged below the 'grader' logger. + + Returns a callable producing the captured records formatted as 'LEVEL:logger:message'. + """ + caplog.set_level(1, logger="grader") + return lambda: [f"{record.levelname}:{record.name}:{record.getMessage()}" for record in caplog.records] diff --git a/tests/functional/test_functional.py b/tests/functional/test_functional.py index d2fc1b6..107a7ac 100644 --- a/tests/functional/test_functional.py +++ b/tests/functional/test_functional.py @@ -2,30 +2,31 @@ import os import shutil -import unittest import zipfile from pathlib import Path +import pytest + import grader.utils.constants as const from grader.utils.process import run -class BaseFunctionalTestWithGrader(unittest.TestCase): +class BaseFunctionalTestWithGrader: """Base class for functional tests with the grader.""" repo_url = "https://github.com/fmipython/pygrader" clone_path = "/tmp/pygrader-cloned" - def setUp(self) -> None: + def setup_method(self) -> None: """Set up test fixtures by cloning the grader repository if needed.""" - if os.path.exists(self.clone_path): + if Path(self.clone_path).exists(): return clone_result = run(["git", "clone", self.repo_url, self.clone_path]) if clone_result.returncode != 0: raise RuntimeError(f"Failed to clone the repository: {clone_result.stderr}") - current_branch_result = run(["git", "rev-parse", "--abbrev-ref", "HEAD"], current_directory=os.getcwd()) + current_branch_result = run(["git", "rev-parse", "--abbrev-ref", "HEAD"], current_directory=str(Path.cwd())) if current_branch_result.returncode != 0: raise RuntimeError(f"Failed to get current branch: {current_branch_result.stderr}") @@ -36,19 +37,19 @@ def setUp(self) -> None: raise RuntimeError(f"Failed to checkout branch {current_branch}: {checkout_result.stderr}") # Remove the functional tests from the repo, as they cause issues and time loss. - functional_tests_path = os.path.join(self.clone_path, "tests", "functional", "test_functional.py") - if os.path.exists(functional_tests_path): - os.remove(functional_tests_path) + functional_tests_path = str(Path(self.clone_path, "tests", "functional", "test_functional.py")) + if Path(functional_tests_path).exists(): + Path(functional_tests_path).unlink() - def tearDown(self) -> None: + def teardown_method(self) -> None: """Clean up test fixtures by removing the cloned repository.""" - if not os.path.exists(self.clone_path): + if not Path(self.clone_path).exists(): return shutil.rmtree(self.clone_path) -@unittest.skipIf(os.name == "nt", "Test skipped on Windows") +@pytest.mark.skipif(os.name == "nt", reason="Test skipped on Windows") class TestFunctionalGoodWeatherWithGrader(BaseFunctionalTestWithGrader): """Functional tests for the grader in a good weather scenario.""" @@ -60,8 +61,8 @@ def test_01_full_config_single_run(self) -> None: """ # Arrange log_file = "grader.log" - if os.path.exists(log_file): - os.remove(log_file) + if Path(log_file).exists(): + Path(log_file).unlink() command = build_command(project_path=self.clone_path) # Act @@ -71,17 +72,17 @@ def test_01_full_config_single_run(self) -> None: run_stdout = run_result.stdout # Assert - self.assertEqual(run_returncode, 0, run_stdout) - self.assertTrue(is_score_correct(expected_score=10, target_check="requirements", grader_output=run_stdout)) - self.assertTrue(is_score_correct(expected_score=10, target_check="pylint", grader_output=run_stdout)) - self.assertTrue(is_score_correct(expected_score=10, target_check="type-hints", grader_output=run_stdout)) - self.assertTrue(os.path.exists(log_file), "Log file was not created") - os.remove(log_file) - self.assertNotIn( - "Running checks for student", run_stdout, "Unexpected student id output found when none was provided" + assert run_returncode == 0, run_stdout + assert is_score_correct(expected_score=10, target_check="requirements", grader_output=run_stdout) + assert is_score_correct(expected_score=10, target_check="pylint", grader_output=run_stdout) + assert is_score_correct(expected_score=10, target_check="type-hints", grader_output=run_stdout) + assert Path(log_file).exists(), "Log file was not created" + Path(log_file).unlink() + assert "Running checks for student" not in run_stdout, ( + "Unexpected student id output found when none was provided" ) - @unittest.skip("Coverage check test is too unstable") + @pytest.mark.skip(reason="Coverage check test is too unstable") def test_04_coverage_check(self) -> None: """Verify that the grader runs the coverage check and returns the expected score.""" # Arrange @@ -94,8 +95,8 @@ def test_04_coverage_check(self) -> None: run_stdout = run_result.stdout # Assert - self.assertEqual(run_returncode, 0, run_stdout) - self.assertTrue(is_score_correct(expected_score=8, target_check="coverage", grader_output=run_stdout)) + assert run_returncode == 0, run_stdout + assert is_score_correct(expected_score=8, target_check="coverage", grader_output=run_stdout) def test_06_student_id_single_run(self) -> None: """Verify the student-id log file name and the student-id output line from a single grader run.""" @@ -103,22 +104,22 @@ def test_06_student_id_single_run(self) -> None: student_id = "student123" log_file = f"{student_id}.log" expected_output = f"Running checks for student {student_id}" - if os.path.exists(log_file): - os.remove(log_file) + if Path(log_file).exists(): + Path(log_file).unlink() command = build_command(project_path=self.clone_path, student_id=student_id) # Act run_result = run(command) # Assert - self.assertEqual(run_result.returncode, 0, run_result.stdout) - self.assertTrue(os.path.exists(log_file), f"Log file with student ID '{student_id}' was not created") - os.remove(log_file) - self.assertIn( - expected_output, run_result.stdout, f"Expected output '{expected_output}' not found in the tool's output" + assert run_result.returncode == 0, run_result.stdout + assert Path(log_file).exists(), f"Log file with student ID '{student_id}' was not created" + Path(log_file).unlink() + assert expected_output in run_result.stdout, ( + f"Expected output '{expected_output}' not found in the tool's output" ) - @unittest.skip("Unstable test") + @pytest.mark.skip(reason="Unstable test") def test_09_all_checks_score_one(self) -> None: """Verify that all checks return a score of 1 when using the full_single_point.json configuration.""" # Arrange @@ -132,11 +133,10 @@ def test_09_all_checks_score_one(self) -> None: run_stdout = run_result.stdout # Assert - self.assertEqual(run_returncode, 0, run_stdout) + assert run_returncode == 0, run_stdout for check in ["requirements", "pylint", "type-hints", "coverage"]: - self.assertTrue( - is_score_correct(expected_score=1, target_check=check, grader_output=run_stdout), - f"Check '{check}' did not have the expected score of 1", + assert is_score_correct(expected_score=1, target_check=check, grader_output=run_stdout), ( + f"Check '{check}' did not have the expected score of 1" ) def test_10_only_pylint_check(self) -> None: @@ -152,13 +152,12 @@ def test_10_only_pylint_check(self) -> None: run_stdout = run_result.stdout # Assert - self.assertEqual(run_returncode, 0, run_stdout) - self.assertTrue( - is_score_correct(expected_score=1, target_check="pylint", grader_output=run_stdout), - "Pylint check did not have the expected score of 1", + assert run_returncode == 0, run_stdout + assert is_score_correct(expected_score=1, target_check="pylint", grader_output=run_stdout), ( + "Pylint check did not have the expected score of 1" ) for check in ["requirements", "type-hints", "coverage"]: - self.assertNotIn(f"Check: {check}", run_stdout, f"Unexpected check '{check}' was executed") + assert f"Check: {check}" not in run_stdout, f"Unexpected check '{check}' was executed" def test_13_checks_flag_selects_subset(self) -> None: """Verify that --checks limits execution to the requested checks from the full config.""" @@ -172,16 +171,15 @@ def test_13_checks_flag_selects_subset(self) -> None: run_stdout = run_result.stdout # Assert - self.assertEqual(run_returncode, 0, run_stdout) - self.assertTrue( - is_score_correct(expected_score=10, target_check="requirements", grader_output=run_stdout), - "Requirements check did not have the expected score of 10", + assert run_returncode == 0, run_stdout + assert is_score_correct(expected_score=10, target_check="requirements", grader_output=run_stdout), ( + "Requirements check did not have the expected score of 10" ) for check in ["pylint", "type-hints", "coverage"]: - self.assertNotIn(f"Check: {check}", run_stdout, f"Unexpected check '{check}' was executed") + assert f"Check: {check}" not in run_stdout, f"Unexpected check '{check}' was executed" -@unittest.skipIf(os.name == "nt", "Test skipped on Windows") +@pytest.mark.skipif(os.name == "nt", reason="Test skipped on Windows") class TestFunctionalBadWeatherWithGrader(BaseFunctionalTestWithGrader): """Functional tests for the grader in a bad weather scenario.""" @@ -190,11 +188,11 @@ def test_11_requirements_txt_does_not_exist(self) -> None: # Arrange command = build_command(project_path=self.clone_path) - if os.path.exists(os.path.join(self.clone_path, "requirements.txt")): - os.remove(os.path.join(self.clone_path, "requirements.txt")) + if Path(str(Path(self.clone_path, "requirements.txt"))).exists(): + Path(str(Path(self.clone_path, "requirements.txt"))).unlink() - if os.path.exists(os.path.join(self.clone_path, "pyproject.toml")): - os.remove(os.path.join(self.clone_path, "pyproject.toml")) + if Path(str(Path(self.clone_path, "pyproject.toml"))).exists(): + Path(str(Path(self.clone_path, "pyproject.toml"))).unlink() # Act run_result = run(command) @@ -203,8 +201,8 @@ def test_11_requirements_txt_does_not_exist(self) -> None: run_stdout = run_result.stdout # Assert - self.assertEqual(run_returncode, 0, run_stdout) - self.assertTrue(is_score_correct(expected_score=0, target_check="requirements", grader_output=run_stdout)) + assert run_returncode == 0, run_stdout + assert is_score_correct(expected_score=0, target_check="requirements", grader_output=run_stdout) def test_12_no_config_provided(self) -> None: """Verify that the grader handles the absence of a configuration file gracefully.""" @@ -216,8 +214,8 @@ def test_12_no_config_provided(self) -> None: run_result = run(command) # Assert - self.assertNotEqual(run_result.returncode, 0, "Expected non-zero return code when no config is provided") - self.assertIn("Error with the configuration file", run_result.stdout) + assert run_result.returncode != 0, "Expected non-zero return code when no config is provided" + assert "Error with the configuration file" in run_result.stdout def test_14_no_project_path_provided(self) -> None: """Verify that the grader handles the absence of a project path gracefully.""" @@ -228,14 +226,14 @@ def test_14_no_project_path_provided(self) -> None: run_result = run(command) # Assert - self.assertNotEqual(run_result.returncode, 0, "Expected non-zero return code when no project path is provided") - self.assertIn("error: the following arguments are required: project_root", run_result.stderr) + assert run_result.returncode != 0, "Expected non-zero return code when no project path is provided" + assert "error: the following arguments are required: project_root" in run_result.stderr def test_15_invalid_project_path(self) -> None: """Verify that the grader handles an invalid project path gracefully.""" # Arrange invalid_path = "/tmp/invalid_project_path" - if os.path.exists(invalid_path): + if Path(invalid_path).exists(): shutil.rmtree(invalid_path) command = build_command(project_path=invalid_path) @@ -244,10 +242,10 @@ def test_15_invalid_project_path(self) -> None: run_result = run(command) # Assert - self.assertIn("Project root directory does not exist", run_result.stdout) + assert "Project root directory does not exist" in run_result.stdout -class BaseFunctionalTestWithSampleProject(unittest.TestCase): +class BaseFunctionalTestWithSampleProject: """ Base class for functional tests with a sample project. @@ -258,9 +256,9 @@ class BaseFunctionalTestWithSampleProject(unittest.TestCase): repo_url = "https://github.com/fmipython/pygrader-sample-project" clone_path = "/tmp/sample_project" - def setUp(self) -> None: + def setup_method(self) -> None: """Set up test fixtures by cloning the sample project repository if needed.""" - if os.path.exists(self.clone_path): + if Path(self.clone_path).exists(): return clone_result = run(["git", "clone", self.repo_url, self.clone_path]) @@ -268,13 +266,13 @@ def setUp(self) -> None: raise RuntimeError(f"Failed to clone the repository: {clone_result.stderr}") # Remove the functional tests from the repo, as they cause issues and time loss. - functional_tests_path = os.path.join(self.clone_path, "tests", "test_functional.py") - if os.path.exists(functional_tests_path): - os.remove(functional_tests_path) + functional_tests_path = str(Path(self.clone_path, "tests", "test_functional.py")) + if Path(functional_tests_path).exists(): + Path(functional_tests_path).unlink() - def tearDown(self) -> None: + def teardown_method(self) -> None: """Clean up test fixtures by removing the cloned repository.""" - if not os.path.exists(self.clone_path): + if not Path(self.clone_path).exists(): return shutil.rmtree(self.clone_path) @@ -292,10 +290,9 @@ def test_01_only_pylint(self) -> None: run_result = run(command) # Assert - self.assertEqual(run_result.returncode, 0, run_result.stdout) - self.assertTrue( - is_score_correct(expected_score=1, target_check="pylint", grader_output=run_result.stdout), - "Pylint check did not have the expected score of 1", + assert run_result.returncode == 0, run_result.stdout + assert is_score_correct(expected_score=1, target_check="pylint", grader_output=run_result.stdout), ( + "Pylint check did not have the expected score of 1" ) def test_02_full(self) -> None: @@ -307,13 +304,11 @@ def test_02_full(self) -> None: run_result = run(command) # Assert - self.assertEqual(run_result.returncode, 0, run_result.stdout) - self.assertTrue( - is_score_correct(expected_score=10, target_check="requirements", grader_output=run_result.stdout) - ) - self.assertTrue(is_score_correct(expected_score=7, target_check="pylint", grader_output=run_result.stdout)) - self.assertTrue(is_score_correct(expected_score=8, target_check="type-hints", grader_output=run_result.stdout)) - self.assertTrue(is_score_correct(expected_score=10, target_check="coverage", grader_output=run_result.stdout)) + assert run_result.returncode == 0, run_result.stdout + assert is_score_correct(expected_score=10, target_check="requirements", grader_output=run_result.stdout) + assert is_score_correct(expected_score=7, target_check="pylint", grader_output=run_result.stdout) + assert is_score_correct(expected_score=8, target_check="type-hints", grader_output=run_result.stdout) + assert is_score_correct(expected_score=10, target_check="coverage", grader_output=run_result.stdout) def test_03_full_single_point(self) -> None: """Verify that all checks return a score of 1 when using the full_single_point.json configuration.""" @@ -324,13 +319,11 @@ def test_03_full_single_point(self) -> None: run_result = run(command) # Assert - self.assertEqual(run_result.returncode, 0, run_result.stdout) - self.assertTrue( - is_score_correct(expected_score=1, target_check="requirements", grader_output=run_result.stdout) - ) - self.assertTrue(is_score_correct(expected_score=1, target_check="pylint", grader_output=run_result.stdout)) - self.assertTrue(is_score_correct(expected_score=1, target_check="type-hints", grader_output=run_result.stdout)) - self.assertTrue(is_score_correct(expected_score=1, target_check="coverage", grader_output=run_result.stdout)) + assert run_result.returncode == 0, run_result.stdout + assert is_score_correct(expected_score=1, target_check="requirements", grader_output=run_result.stdout) + assert is_score_correct(expected_score=1, target_check="pylint", grader_output=run_result.stdout) + assert is_score_correct(expected_score=1, target_check="type-hints", grader_output=run_result.stdout) + assert is_score_correct(expected_score=1, target_check="coverage", grader_output=run_result.stdout) def test_04_structure(self) -> None: """ @@ -345,12 +338,12 @@ def test_04_structure(self) -> None: run_result = run(command) # Assert - self.assertEqual(run_result.returncode, 0, run_result.stdout) - self.assertTrue( - is_non_scored_check_correct(expected_result=True, target_check="structure", grader_output=run_result.stdout) + assert run_result.returncode == 0, run_result.stdout + assert is_non_scored_check_correct( + expected_result=True, target_check="structure", grader_output=run_result.stdout ) - @unittest.skip("The tests for sample_project are not in the repo") + @pytest.mark.skip(reason="The tests for sample_project are not in the repo") def test_05_tests(self) -> None: """Verify that the tests are executed and return the expected score when using the tests.json configuration.""" # Arrange @@ -360,8 +353,8 @@ def test_05_tests(self) -> None: run_result = run(command) # Assert - self.assertEqual(run_result.returncode, 0, run_result.stdout) - self.assertTrue(is_score_correct(expected_score=13, target_check="tests", grader_output=run_result.stdout)) + assert run_result.returncode == 0, run_result.stdout + assert is_score_correct(expected_score=13, target_check="tests", grader_output=run_result.stdout) def test_06_2024(self) -> None: """Verify that the checks are executed and return the expected scores when using the 2024.json configuration.""" @@ -372,14 +365,12 @@ def test_06_2024(self) -> None: run_result = run(command) # Assert - self.assertEqual(run_result.returncode, 0, run_result.stdout) + assert run_result.returncode == 0, run_result.stdout - self.assertTrue(is_score_correct(expected_score=2, target_check="pylint", grader_output=run_result.stdout)) - self.assertTrue(is_score_correct(expected_score=3, target_check="type-hints", grader_output=run_result.stdout)) - self.assertTrue(is_score_correct(expected_score=5, target_check="coverage", grader_output=run_result.stdout)) - self.assertTrue( - is_score_correct(expected_score=1, target_check="requirements", grader_output=run_result.stdout) - ) + assert is_score_correct(expected_score=2, target_check="pylint", grader_output=run_result.stdout) + assert is_score_correct(expected_score=3, target_check="type-hints", grader_output=run_result.stdout) + assert is_score_correct(expected_score=5, target_check="coverage", grader_output=run_result.stdout) + assert is_score_correct(expected_score=1, target_check="requirements", grader_output=run_result.stdout) class TestRemoteTests(BaseFunctionalTestWithSampleProject): @@ -392,10 +383,10 @@ def test_01_remote_tests(self) -> None: Uses the tests.json configuration. """ # Arrange - path_to_tests = os.path.join(self.clone_path, "tests", "test_sample_code.py") + path_to_tests = str(Path(self.clone_path, "tests", "test_sample_code.py")) - if os.path.exists(path_to_tests): - os.remove(path_to_tests) + if Path(path_to_tests).exists(): + Path(path_to_tests).unlink() command = build_command(project_path=self.clone_path, config_file="tests.json") @@ -405,8 +396,8 @@ def test_01_remote_tests(self) -> None: # input("press any key to continue...") # Assert - self.assertEqual(run_result.returncode, 0, run_result.stdout) - self.assertTrue(is_score_correct(expected_score=13.5, target_check="tests", grader_output=run_result.stdout)) + assert run_result.returncode == 0, run_result.stdout + assert is_score_correct(expected_score=13.5, target_check="tests", grader_output=run_result.stdout) class TestZipFileOnSampleProject(BaseFunctionalTestWithSampleProject): @@ -429,10 +420,9 @@ def test_01_zip_archive_passed(self) -> None: run_result = run(command) # Assert - self.assertEqual(run_result.returncode, 0, run_result.stdout) - self.assertTrue( - is_score_correct(expected_score=1, target_check="pylint", grader_output=run_result.stdout), - "Pylint check did not have the expected score of 1", + assert run_result.returncode == 0, run_result.stdout + assert is_score_correct(expected_score=1, target_check="pylint", grader_output=run_result.stdout), ( + "Pylint check did not have the expected score of 1" ) @@ -445,19 +435,19 @@ class TestMultipleProjectsSupport(BaseFunctionalTestWithSampleProject): that's the shape ``desktop.utils.extract_student_id_from_path`` expects. """ - def setUp(self) -> None: + def setup_method(self) -> None: """Set up the sample project checkout plus a scratch directory for batch fixtures.""" - super().setUp() + super().setup_method() self.batch_dir = Path(self.clone_path).parent / "batch_projects" if self.batch_dir.exists(): shutil.rmtree(self.batch_dir) self.batch_dir.mkdir(parents=True) - def tearDown(self) -> None: + def teardown_method(self) -> None: """Clean up the batch scratch directory in addition to the sample project checkout.""" if self.batch_dir.exists(): shutil.rmtree(self.batch_dir) - super().tearDown() + super().teardown_method() def test_01_same_filename_archives_are_graded_independently(self) -> None: """ @@ -499,19 +489,13 @@ def test_01_same_filename_archives_are_graded_independently(self) -> None: run_result = run(command) # Assert - self.assertEqual(run_result.returncode, 0, run_result.stdout) - self.assertTrue( - is_score_correct_for_run( - expected_score=1, target_check="pylint", run_id="CLEANID", grader_output=run_result.stdout - ), - "Clean submission did not keep its own pylint score", - ) - self.assertTrue( - is_score_correct_for_run( - expected_score=0, target_check="pylint", run_id="BROKENID", grader_output=run_result.stdout - ), - "Broken submission did not get its own (lower) pylint score - archives may be overwriting each other", - ) + assert run_result.returncode == 0, run_result.stdout + assert is_score_correct_for_run( + expected_score=1, target_check="pylint", run_id="CLEANID", grader_output=run_result.stdout + ), "Clean submission did not keep its own pylint score" + assert is_score_correct_for_run( + expected_score=0, target_check="pylint", run_id="BROKENID", grader_output=run_result.stdout + ), "Broken submission did not get its own (lower) pylint score - archives may be overwriting each other" def test_02_venv_failure_for_one_project_does_not_stop_the_batch(self) -> None: """Verify that a submission whose dependencies fail to install doesn't block the rest of the batch.""" @@ -531,17 +515,12 @@ def test_02_venv_failure_for_one_project_does_not_stop_the_batch(self) -> None: run_result = run(command) # Assert - self.assertEqual(run_result.returncode, 1, run_result.stdout) - self.assertTrue( - is_score_correct_for_run( - expected_score=1, target_check="pylint", run_id="GOODID", grader_output=run_result.stdout - ), - "Good submission was not graded despite the other submission's venv failure", - ) - self.assertNotIn( - "Run ID: BADID", - run_result.stdout, - "Submission with an unresolvable dependency should not have produced any results", + assert run_result.returncode == 1, run_result.stdout + assert is_score_correct_for_run( + expected_score=1, target_check="pylint", run_id="GOODID", grader_output=run_result.stdout + ), "Good submission was not graded despite the other submission's venv failure" + assert "Run ID: BADID" not in run_result.stdout, ( + "Submission with an unresolvable dependency should not have produced any results" ) @@ -562,8 +541,8 @@ def build_command( """ grader_entrypoint = "pygrader.py" - full_config_path = os.path.join(const.CONFIG_DIR, config_file) - base_command = ["uv", "run", os.path.join(const.ROOT_DIR, grader_entrypoint)] + full_config_path = str(Path(const.CONFIG_DIR, config_file)) + base_command = ["uv", "run", str(Path(const.ROOT_DIR, grader_entrypoint))] command = base_command + ["--config", full_config_path] if project_path is not None: diff --git a/tests/functional/test_wheel.py b/tests/functional/test_wheel.py index 531e0bc..08133d3 100644 --- a/tests/functional/test_wheel.py +++ b/tests/functional/test_wheel.py @@ -1,24 +1,25 @@ """Module for functional tests related to the wheel package.""" -import unittest from pathlib import Path from tempfile import TemporaryDirectory +import pytest + from grader.utils.process import run -class TestWheel(unittest.TestCase): +class TestWheel: """Class for testing wheel package functionality.""" temp_dir: TemporaryDirectory @classmethod - def setUpClass(cls) -> None: + def setup_class(cls) -> None: """Create a temporary directory for wheel building tests.""" cls.temp_dir = TemporaryDirectory() @classmethod - def tearDownClass(cls) -> None: + def teardown_class(cls) -> None: """Clean up the temporary directory after tests.""" cls.temp_dir.cleanup() @@ -27,9 +28,9 @@ def test_01_wheel_build(self) -> None: try: wheel_path = build_wheel(self.temp_dir.name) except RuntimeError as e: - self.fail(f"Wheel build failed with error: {e}") + pytest.fail(f"Wheel build failed with error: {e}") - self.assertTrue(wheel_path.exists(), "Wheel file was not created") + assert wheel_path.exists(), "Wheel file was not created" def build_wheel(build_dir: str) -> Path: diff --git a/tests/unit/test_abstract_check.py b/tests/unit/test_abstract_check.py index a4f92f0..62a0e3e 100644 --- a/tests/unit/test_abstract_check.py +++ b/tests/unit/test_abstract_check.py @@ -1,8 +1,9 @@ """Unit tests for the AbstractCheck class in the abstract_check module.""" -import unittest from unittest.mock import MagicMock, call, patch +import pytest + from grader.checks.abstract_check import AbstractCheck from grader.exceptions import CheckError from grader.models.check_result import CheckResult @@ -17,7 +18,7 @@ def run(self) -> CheckResult[int]: return CheckResult("dummy", 0, "", "") -class TestAbstractCheck(unittest.TestCase): +class TestAbstractCheck: """Test cases for the AbstractCheck class.""" def test_01_run_without_venv_when_required(self) -> None: @@ -26,7 +27,7 @@ def test_01_run_without_venv_when_required(self) -> None: check = DummyCheck("dummy", "dummy", is_venv_required=True) # Act & Assert - with self.assertRaises(CheckError): + with pytest.raises(CheckError): check.run() def test_02_assets_default_to_empty_list(self) -> None: @@ -35,7 +36,7 @@ def test_02_assets_default_to_empty_list(self) -> None: check = DummyCheck("dummy", "dummy") # Assert - self.assertEqual(check.assets, []) + assert check.assets == [] @patch("grader.checks.abstract_check.Resource") def test_03_assets_are_wrapped_in_resource(self, mocked_resource: MagicMock) -> None: @@ -48,4 +49,4 @@ def test_03_assets_are_wrapped_in_resource(self, mocked_resource: MagicMock) -> # Assert mocked_resource.assert_has_calls([call(source) for source in sources]) - self.assertEqual(check.assets, [mocked_resource.return_value, mocked_resource.return_value]) + assert check.assets == [mocked_resource.return_value, mocked_resource.return_value] diff --git a/tests/unit/test_checks_factory.py b/tests/unit/test_checks_factory.py index f76c586..536da8b 100644 --- a/tests/unit/test_checks_factory.py +++ b/tests/unit/test_checks_factory.py @@ -1,13 +1,15 @@ """Unit tests for the checks_factory module in the grader package.""" -import unittest from unittest.mock import MagicMock, patch +import pytest + from grader.checks.checks_factory import create_checks +from grader.checks.complexity_check import ComplexityCheck from grader.exceptions import InvalidCheckError, InvalidConfigError -class TestChecksFactory(unittest.TestCase): +class TestChecksFactory: """Unit tests for the create_checks function in the checks_factory module.""" def test_01_no_checks_in_config(self) -> None: @@ -17,7 +19,7 @@ def test_01_no_checks_in_config(self) -> None: project_root = "test_project" # Act - with self.assertRaises(InvalidConfigError): + with pytest.raises(InvalidConfigError): create_checks(config, project_root) def test_02_invalid_check_configuration(self) -> None: @@ -27,7 +29,7 @@ def test_02_invalid_check_configuration(self) -> None: project_root = "test_project" # Act - with self.assertRaises(InvalidConfigError): + with pytest.raises(InvalidConfigError): create_checks(config, project_root) def test_03_max_points_missing(self) -> None: @@ -37,7 +39,7 @@ def test_03_max_points_missing(self) -> None: project_root = "test_project" # Act - with self.assertRaises(InvalidConfigError): + with pytest.raises(InvalidConfigError): create_checks(config, project_root) def test_04_name_missing(self) -> None: @@ -47,7 +49,7 @@ def test_04_name_missing(self) -> None: project_root = "test_project" # Act - with self.assertRaises(InvalidConfigError): + with pytest.raises(InvalidConfigError): create_checks(config, project_root) def test_05_unknown_check_name(self) -> None: @@ -57,10 +59,19 @@ def test_05_unknown_check_name(self) -> None: project_root = "test_project" # Act - with self.assertRaises(InvalidCheckError): + with pytest.raises(InvalidCheckError): create_checks(config, project_root) - def test_06_venv_required(self) -> None: + def test_06_complexity_check_is_created_with_its_options(self) -> None: + """Test that the complexity check is built from its config, without max_points.""" + config = {"checks": [{"name": "complexity", "is_venv_required": False, "max_complexity": 7}]} + + non_venv_checks, venv_checks = create_checks(config, "test_project") + + assert not venv_checks + assert isinstance(non_venv_checks[0], ComplexityCheck) + + def test_07_venv_required(self) -> None: """Test that checks requiring a virtual environment are separated correctly.""" # Arrange config = {"checks": [{"name": "coverage", "max_points": 10, "is_venv_required": True}]} @@ -70,10 +81,10 @@ def test_06_venv_required(self) -> None: non_venv_checks, venv_checks = create_checks(config, project_root) # Assert - self.assertEqual(len(non_venv_checks), 0) - self.assertEqual(len(venv_checks), 1) + assert len(non_venv_checks) == 0 + assert len(venv_checks) == 1 - def test_07_venv_not_required(self) -> None: + def test_08_venv_not_required(self) -> None: """Test that checks not requiring a virtual environment are separated correctly.""" # Arrange config = {"checks": [{"name": "coverage", "max_points": 10, "is_venv_required": False}]} @@ -83,20 +94,20 @@ def test_07_venv_not_required(self) -> None: non_venv_checks, venv_checks = create_checks(config, project_root) # Assert - self.assertEqual(len(non_venv_checks), 1) - self.assertEqual(len(venv_checks), 0) + assert len(non_venv_checks) == 1 + assert len(venv_checks) == 0 - def test_08_is_venv_required_not_present(self) -> None: + def test_09_is_venv_required_not_present(self) -> None: """Test that when is_venv_required is not present, an InvalidConfigError is raised.""" # Arrange config = {"checks": [{"name": "coverage", "max_points": 10}]} project_root = "test_project" # Act - with self.assertRaises(InvalidConfigError): + with pytest.raises(InvalidConfigError): _ = create_checks(config, project_root) - def test_09_environment_variables_global_only(self) -> None: + def test_10_environment_variables_global_only(self) -> None: """Test that global environment variables are passed to checks.""" # Arrange config = { @@ -109,16 +120,16 @@ def test_09_environment_variables_global_only(self) -> None: non_venv_checks, _ = create_checks(config, project_root) # Assert - self.assertEqual(len(non_venv_checks), 1) + assert len(non_venv_checks) == 1 check = non_venv_checks[0] - self.assertIsNotNone(check.env_vars) + assert check.env_vars is not None # To keep the linter happy if check.env_vars is not None: - self.assertIn("GLOBAL_VAR", check.env_vars) - self.assertEqual(check.env_vars["GLOBAL_VAR"], "global_value") + assert "GLOBAL_VAR" in check.env_vars + assert check.env_vars["GLOBAL_VAR"] == "global_value" - def test_10_environment_variables_check_specific(self) -> None: + def test_11_environment_variables_check_specific(self) -> None: """Test that check-specific environment variables are passed to checks.""" # Arrange config = { @@ -137,16 +148,16 @@ def test_10_environment_variables_check_specific(self) -> None: non_venv_checks, _ = create_checks(config, project_root) # Assert - self.assertEqual(len(non_venv_checks), 1) + assert len(non_venv_checks) == 1 check = non_venv_checks[0] - self.assertIsNotNone(check.env_vars) + assert check.env_vars is not None # To keep the linter happy if check.env_vars is not None: - self.assertIn("CHECK_VAR", check.env_vars) - self.assertEqual(check.env_vars["CHECK_VAR"], "check_value") + assert "CHECK_VAR" in check.env_vars + assert check.env_vars["CHECK_VAR"] == "check_value" - def test_11_environment_variables_merge_priority(self) -> None: + def test_12_environment_variables_merge_priority(self) -> None: """Test that check-specific environment variables override global ones.""" # Arrange config = { @@ -166,21 +177,21 @@ def test_11_environment_variables_merge_priority(self) -> None: non_venv_checks, _ = create_checks(config, project_root) # Assert - self.assertEqual(len(non_venv_checks), 1) + assert len(non_venv_checks) == 1 check = non_venv_checks[0] - self.assertIsNotNone(check.env_vars) + assert check.env_vars is not None # To keep the linter happy if check.env_vars is not None: # Check that check-specific API_KEY overrides global - self.assertEqual(check.env_vars["API_KEY"], "check_key") + assert check.env_vars["API_KEY"] == "check_key" # Check that global variable is still present - self.assertEqual(check.env_vars["GLOBAL_VAR"], "global_value") + assert check.env_vars["GLOBAL_VAR"] == "global_value" # Check that check-specific variable is present - self.assertEqual(check.env_vars["CHECK_VAR"], "check_value") + assert check.env_vars["CHECK_VAR"] == "check_value" @patch("os.environ") - def test_12_environment_variables_none_when_not_defined(self, existing_env: MagicMock) -> None: + def test_13_environment_variables_none_when_not_defined(self, existing_env: MagicMock) -> None: """Test that env_vars is None when no environment variables are defined.""" # Arrange existing_env.return_value = {} @@ -191,11 +202,11 @@ def test_12_environment_variables_none_when_not_defined(self, existing_env: Magi non_venv_checks, _ = create_checks(config, project_root) # Assert - self.assertEqual(len(non_venv_checks), 1) + assert len(non_venv_checks) == 1 check = non_venv_checks[0] - self.assertEqual(check.env_vars, {}) + assert check.env_vars == {} - def test_13_selected_checks_filters_subset(self) -> None: + def test_14_selected_checks_filters_subset(self) -> None: """Test that selected_checks limits the built checks to the matching subset.""" # Arrange config = { @@ -211,10 +222,10 @@ def test_13_selected_checks_filters_subset(self) -> None: non_venv_checks, venv_checks = create_checks(config, project_root, selected_checks=["requirements", "pylint"]) # Assert - self.assertEqual([check.name for check in non_venv_checks], ["requirements"]) - self.assertEqual([check.name for check in venv_checks], ["pylint"]) + assert [check.name for check in non_venv_checks] == ["requirements"] + assert [check.name for check in venv_checks] == ["pylint"] - def test_14_selected_checks_none_runs_everything(self) -> None: + def test_15_selected_checks_none_runs_everything(self) -> None: """Test that selected_checks=None keeps the current full-run behavior.""" # Arrange config = { @@ -229,21 +240,21 @@ def test_14_selected_checks_none_runs_everything(self) -> None: non_venv_checks, venv_checks = create_checks(config, project_root, selected_checks=None) # Assert - self.assertEqual(len(non_venv_checks), 1) - self.assertEqual(len(venv_checks), 1) + assert len(non_venv_checks) == 1 + assert len(venv_checks) == 1 - def test_15_selected_checks_unknown_name_raises(self) -> None: + def test_16_selected_checks_unknown_name_raises(self) -> None: """Test that requesting an unknown check name raises InvalidCheckError.""" # Arrange config = {"checks": [{"name": "coverage", "max_points": 10, "is_venv_required": False}]} project_root = "test_project" # Act & Assert - with self.assertRaises(InvalidCheckError): + with pytest.raises(InvalidCheckError): create_checks(config, project_root, selected_checks=["pylintt"]) @patch("grader.checks.abstract_check.Resource") - def test_16_assets_are_loaded_as_resources(self, mocked_resource: MagicMock) -> None: + def test_17_assets_are_loaded_as_resources(self, mocked_resource: MagicMock) -> None: """Test that a check's "assets" config entries are converted to Resource objects.""" # Arrange config = { @@ -262,11 +273,11 @@ def test_16_assets_are_loaded_as_resources(self, mocked_resource: MagicMock) -> non_venv_checks, _ = create_checks(config, project_root) # Assert - self.assertEqual(len(non_venv_checks), 1) + assert len(non_venv_checks) == 1 check = non_venv_checks[0] - self.assertEqual(check.assets, [mocked_resource.return_value, mocked_resource.return_value]) + assert check.assets == [mocked_resource.return_value, mocked_resource.return_value] - def test_17_assets_default_to_empty_list(self) -> None: + def test_18_assets_default_to_empty_list(self) -> None: """Test that a check built without an "assets" key exposes an empty assets list.""" # Arrange config = {"checks": [{"name": "coverage", "max_points": 10, "is_venv_required": False}]} @@ -276,5 +287,5 @@ def test_17_assets_default_to_empty_list(self) -> None: non_venv_checks, _ = create_checks(config, project_root) # Assert - self.assertEqual(len(non_venv_checks), 1) - self.assertEqual(non_venv_checks[0].assets, []) + assert len(non_venv_checks) == 1 + assert non_venv_checks[0].assets == [] diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index af01ed3..4622ce8 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -1,6 +1,5 @@ """Unit tests for the CLI module in the cli.py module.""" -import unittest from unittest.mock import patch from desktop.cli import get_args @@ -8,57 +7,53 @@ # FILE: grader/utils/test_cli.py -class TestGetArgs(unittest.TestCase): +class TestGetArgs: """Unit tests for the get_args function.""" @patch("sys.argv", ["cli.py", "path/to/project"]) def test_01_required_argument(self) -> None: """Test 01: Test that the required argument is parsed correctly.""" expected = ("project_root", "path/to/project") - self.assertIn(expected, get_args().items()) + assert expected in get_args().items() @patch("sys.argv", ["cli.py", "path/to/project", "-c", "path/to/config"]) def test_02_optional_config_argument(self) -> None: """Test 02: Test that the optional config argument is parsed correctly.""" expected = ("config", "path/to/config") - self.assertIn(expected, get_args().items()) + assert expected in get_args().items() @patch("sys.argv", ["cli.py", "path/to/project", "--student-id", "12345"]) def test_03_optional_student_id_argument(self) -> None: """Test 03: Test that the optional student ID argument is parsed correctly.""" expected = ("student_id", "12345") - self.assertIn(expected, get_args().items()) + assert expected in get_args().items() @patch("sys.argv", ["cli.py", "path/to/project", "-v"]) def test_04_verbosity_argument(self) -> None: """Test 04: Test that the verbosity argument is parsed correctly.""" expected = ("verbosity", 1) - self.assertIn(expected, get_args().items()) + assert expected in get_args().items() @patch("sys.argv", ["cli.py", "path/to/project", "-vv"]) def test_05_multiple_verbosity_argument(self) -> None: """Test 05: Test that multiple verbosity arguments are parsed correctly.""" expected = ("verbosity", 2) - self.assertIn(expected, get_args().items()) + assert expected in get_args().items() @patch("sys.argv", ["cli.py", "path/to/project", "--checks", "pylint,coverage"]) def test_06_checks_argument(self) -> None: """Test 06: Test that the checks argument is parsed into a list of names.""" expected = ("checks", ["pylint", "coverage"]) - self.assertIn(expected, get_args().items()) + assert expected in get_args().items() @patch("sys.argv", ["cli.py", "path/to/project"]) def test_07_checks_argument_omitted(self) -> None: """Test 07: Test that the checks argument defaults to None when omitted.""" expected = ("checks", None) - self.assertIn(expected, get_args().items()) + assert expected in get_args().items() @patch("sys.argv", ["cli.py", "path/to/project", "--checks", " pylint , coverage "]) def test_08_checks_argument_strips_whitespace(self) -> None: """Test 08: Test that whitespace around check names is stripped.""" expected = ("checks", ["pylint", "coverage"]) - self.assertIn(expected, get_args().items()) - - -if __name__ == "__main__": - unittest.main() + assert expected in get_args().items() diff --git a/tests/unit/test_complexity_check.py b/tests/unit/test_complexity_check.py new file mode 100644 index 0000000..f8ca6bd --- /dev/null +++ b/tests/unit/test_complexity_check.py @@ -0,0 +1,83 @@ +"""Unit tests for the ComplexityCheck class in the complexity_check module.""" + +from unittest.mock import MagicMock, patch + +import pytest + +from grader.checks.complexity_check import ComplexityCheck +from grader.exceptions import CheckError +from grader.models.check_result import NonScoredCheckResult + + +def _file_complexity(*functions: tuple[str, int, int]) -> MagicMock: + """Build a fake complexipy file result from (name, line_start, complexity) tuples.""" + result = MagicMock() + result.functions = [MagicMock(line_start=line, complexity=complexity) for _, line, complexity in functions] + for mock_function, (name, _, _) in zip(result.functions, functions): + mock_function.name = name + return result + + +@pytest.fixture(name="check") +def fixture_check() -> ComplexityCheck: + """Create a complexity check with a threshold of 10.""" + return ComplexityCheck("complexity", "sample_dir", max_complexity=10) + + +@patch("complexipy.file_complexity") +@patch("grader.utils.files.find_all_source_files") +def test_01_all_functions_within_threshold( + mock_find: MagicMock, mock_complexity: MagicMock, check: ComplexityCheck +) -> None: + """Verify that the check passes when no function exceeds the threshold.""" + mock_find.return_value = ["a.py"] + mock_complexity.return_value = _file_complexity(("foo", 1, 10), ("bar", 5, 2)) + + result = check.run() + + assert result == NonScoredCheckResult("complexity", True, "All functions are within complexity 10", "") + + +@patch("complexipy.file_complexity") +@patch("grader.utils.files.find_all_source_files") +def test_02_function_above_threshold(mock_find: MagicMock, mock_complexity: MagicMock, check: ComplexityCheck) -> None: + """Verify that the check fails and names the offending functions.""" + mock_find.return_value = ["a.py", "b.py"] + mock_complexity.side_effect = [_file_complexity(("foo", 1, 11)), _file_complexity(("bar", 7, 3))] + + result = check.run() + + assert result == NonScoredCheckResult("complexity", False, "Functions above complexity 10: a.py:1 foo (11)", "") + + +@patch("grader.utils.files.find_all_source_files") +def test_03_no_source_files(mock_find: MagicMock, check: ComplexityCheck) -> None: + """Verify that a project without source files passes.""" + mock_find.return_value = [] + + assert check.run().result is True + + +@patch("grader.utils.files.find_all_source_files") +def test_04_finding_files_fails(mock_find: MagicMock, check: ComplexityCheck) -> None: + """Verify that an OSError while finding files is reported as a CheckError.""" + mock_find.side_effect = OSError("boom") + + with pytest.raises(CheckError): + check.run() + + +@patch("complexipy.file_complexity") +@patch("grader.utils.files.find_all_source_files") +def test_05_unparsable_file_fails_but_others_are_analysed( + mock_find: MagicMock, mock_complexity: MagicMock, check: ComplexityCheck +) -> None: + """Verify that a file complexipy cannot parse fails the check without hiding the other files.""" + mock_find.return_value = ["bad.py", "a.py"] + mock_complexity.side_effect = [ValueError("bad syntax"), _file_complexity(("foo", 1, 11))] + + result = check.run() + + assert result == NonScoredCheckResult( + "complexity", False, "Functions above complexity 10: a.py:1 foo (11). Could not analyse: bad.py", "" + ) diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index 5ef85fd..962f57c 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -1,13 +1,14 @@ """Unit tests for the config module.""" -import unittest from unittest.mock import MagicMock, mock_open, patch +import pytest + from grader.exceptions import InvalidConfigError, ResourceError from grader.utils.config import load_config, read_from_file -class TestLoadConfig(unittest.TestCase): +class TestLoadConfig: """Unit tests for load_config.""" @patch("grader.utils.config.read_from_file") @@ -25,7 +26,7 @@ def test_01_resolves_resource_to_file_and_reads_it( config = load_config(sample_config_path) # Assert - self.assertEqual(config, {"key": "value"}) + assert config == {"key": "value"} mock_resource_cls.assert_called_once_with(sample_config_path) mock_read_from_file.assert_called_once_with("/tmp/config.json") @@ -36,11 +37,11 @@ def test_02_resource_error_propagates(self, mock_resource_cls: MagicMock) -> Non mock_resource_cls.return_value.to_file.side_effect = ResourceError("Download failed") # Act & Assert - with self.assertRaises(ResourceError): + with pytest.raises(ResourceError): load_config("http://example.com/config.json") -class TestReadFromFile(unittest.TestCase): +class TestReadFromFile: """Unit tests for read_from_file.""" @patch("builtins.open", new_callable=mock_open, read_data='{"key": "value"}') @@ -53,7 +54,7 @@ def test_01_local_file_loaded_successfully(self, mock_file: MagicMock) -> None: config = read_from_file(sample_config_path) # Assert - self.assertEqual(config, {"key": "value"}) + assert config == {"key": "value"} mock_file.assert_called_once_with(sample_config_path, encoding="utf-8") @patch("builtins.open", new_callable=mock_open) @@ -63,7 +64,7 @@ def test_02_local_file_not_found(self, mock_file: MagicMock) -> None: mock_file.side_effect = FileNotFoundError # Act & Assert - with self.assertRaises(InvalidConfigError): + with pytest.raises(InvalidConfigError): read_from_file("non_existent_config.json") @patch("builtins.open", new_callable=mock_open, read_data='{"key": "value"') @@ -72,5 +73,5 @@ def test_03_local_file_invalid_json(self, _: MagicMock) -> None: # The read_data is intentionally malformed JSON # Act & Assert - with self.assertRaises(InvalidConfigError): + with pytest.raises(InvalidConfigError): read_from_file("invalid_config.json") diff --git a/tests/unit/test_coverage_check.py b/tests/unit/test_coverage_check.py index d8d7e90..e127d18 100644 --- a/tests/unit/test_coverage_check.py +++ b/tests/unit/test_coverage_check.py @@ -1,40 +1,40 @@ """Unit tests for the CoverageCheck class in the coverage_check module.""" -import unittest +from collections.abc import Callable from subprocess import CompletedProcess from unittest.mock import MagicMock, patch +import pytest + from grader.checks.coverage_check import CoverageCheck from grader.exceptions import CheckError from grader.models.check_result import ScoredCheckResult -class TestCoverageCheck(unittest.TestCase): +class TestCoverageCheck: """Test cases for the CoverageCheck class.""" - def setUp(self) -> None: + def setup_method(self) -> None: """Set up the CoverageCheck instance for testing.""" self.coverage_check = CoverageCheck("Coverage", "sample_dir", 2, is_venv_required=False) # This way, we have 3 ranges: 0-33, 34-66, 67-100 - return super().setUp() @patch("subprocess.run") - def test_01_coverage_run_fail(self, mocked_run: MagicMock) -> None: + def test_01_coverage_run_fail(self, mocked_run: MagicMock, log_output: Callable[[], list[str]]) -> None: """Test that a failed coverage run logs an error and raises an exception.""" # Arrange mocked_run.return_value = CompletedProcess(args=["coverage", "run"], returncode=1) # Act - with self.assertLogs("grader", level="ERROR") as log: - with self.assertRaises(CheckError): - self.coverage_check.run() - is_message_logged = "ERROR:grader:Coverage run failed. stdout:" in log.output[0] + with pytest.raises(CheckError): + self.coverage_check.run() + is_message_logged = "ERROR:grader:Coverage run failed. stdout:" in " ".join(log_output()) # Assert - self.assertTrue(is_message_logged) + assert is_message_logged @patch("subprocess.run") - def test_02_coverage_report_fail(self, mocked_run: MagicMock) -> None: + def test_02_coverage_report_fail(self, mocked_run: MagicMock, log_output: Callable[[], list[str]]) -> None: """Test that a failed coverage report logs an error and returns a score of 0.0.""" # Arrange @@ -47,13 +47,12 @@ def mocked_run_side_effect(*args: list, **_: dict) -> CompletedProcess | None: mocked_run.side_effect = mocked_run_side_effect # Act - with self.assertLogs("grader", level="ERROR") as log: - with self.assertRaises(CheckError): - self.coverage_check.run() - is_message_logged = "ERROR:grader:Coverage report failed" in log.output + with pytest.raises(CheckError): + self.coverage_check.run() + is_message_logged = "ERROR:grader:Coverage report failed" in log_output() # Assert - self.assertTrue(is_message_logged) + assert is_message_logged @patch("grader.checks.coverage_check.CoverageCheck._CoverageCheck__coverage_run") @patch("grader.checks.coverage_check.CoverageCheck._CoverageCheck__coverage_report") @@ -68,7 +67,7 @@ def test_03_translate_score_zero(self, mocked_report: MagicMock, mocked_run: Mag actual_score = self.coverage_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("grader.checks.coverage_check.CoverageCheck._CoverageCheck__coverage_run") @patch("grader.checks.coverage_check.CoverageCheck._CoverageCheck__coverage_report") @@ -83,7 +82,7 @@ def test_04_translate_score_inside_first_range(self, mocked_report: MagicMock, m actual_score = self.coverage_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("grader.checks.coverage_check.CoverageCheck._CoverageCheck__coverage_run") @patch("grader.checks.coverage_check.CoverageCheck._CoverageCheck__coverage_report") @@ -98,7 +97,7 @@ def test_05_translate_score_right_bound_first_range(self, mocked_report: MagicMo actual_score = self.coverage_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("grader.checks.coverage_check.CoverageCheck._CoverageCheck__coverage_run") @patch("grader.checks.coverage_check.CoverageCheck._CoverageCheck__coverage_report") @@ -113,7 +112,7 @@ def test_06_translate_score_left_bound_second_range(self, mocked_report: MagicMo actual_score = self.coverage_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("grader.checks.coverage_check.CoverageCheck._CoverageCheck__coverage_run") @patch("grader.checks.coverage_check.CoverageCheck._CoverageCheck__coverage_report") @@ -130,7 +129,7 @@ def test_07_translate_score_inside_bound_second_range( actual_score = self.coverage_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("grader.checks.coverage_check.CoverageCheck._CoverageCheck__coverage_run") @patch("grader.checks.coverage_check.CoverageCheck._CoverageCheck__coverage_report") @@ -145,7 +144,7 @@ def test_08_translate_score_right_bound_second_range(self, mocked_report: MagicM actual_score = self.coverage_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("grader.checks.coverage_check.CoverageCheck._CoverageCheck__coverage_run") @patch("grader.checks.coverage_check.CoverageCheck._CoverageCheck__coverage_report") @@ -160,7 +159,7 @@ def test_09_translate_score_inside_bound_third_range(self, mocked_report: MagicM actual_score = self.coverage_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("grader.checks.coverage_check.CoverageCheck._CoverageCheck__coverage_run") @patch("grader.checks.coverage_check.CoverageCheck._CoverageCheck__coverage_report") @@ -175,7 +174,7 @@ def test_10_translate_score_max(self, mocked_report: MagicMock, mocked_run: Magi actual_score = self.coverage_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("subprocess.run") def test_11_coverage_report_read_properly(self, mocked_run: MagicMock) -> None: @@ -194,4 +193,4 @@ def mocked_run_side_effect(*args: list, **_: dict) -> CompletedProcess | None: result = self.coverage_check.run() # Assert - self.assertEqual(ScoredCheckResult("Coverage", 2, "Tests cover 100.00% of the code", "", 2), result) + assert ScoredCheckResult("Coverage", 2, "Tests cover 100.00% of the code", "", 2) == result diff --git a/tests/unit/test_environment.py b/tests/unit/test_environment.py index 0708bdd..f37e26a 100644 --- a/tests/unit/test_environment.py +++ b/tests/unit/test_environment.py @@ -1,21 +1,20 @@ """Unit tests for the environment module in the grader.utils package.""" import os -import unittest from unittest.mock import MagicMock, patch from grader.utils.environment import merge_environment_variables -class TestEnvironmentMerging(unittest.TestCase): +class TestEnvironmentMerging: """Unit tests for the merge_environment_variables function.""" - def setUp(self) -> None: + def setup_method(self) -> None: """Set up test environment.""" # Save original environment self.original_env = dict(os.environ) - def tearDown(self) -> None: + def teardown_method(self) -> None: """Restore original environment.""" os.environ.clear() os.environ.update(self.original_env) @@ -26,7 +25,7 @@ def test_01_both_none_returns_none(self) -> None: result = merge_environment_variables(None, None) # Assert - self.assertEqual(result, {}) + assert result == {} @patch("os.environ") def test_02_both_empty_returns_none(self, existing_env: MagicMock) -> None: @@ -38,7 +37,7 @@ def test_02_both_empty_returns_none(self, existing_env: MagicMock) -> None: result = merge_environment_variables({}, {}) # Assert - self.assertEqual(result, {}) + assert result == {} def test_03_global_only(self) -> None: """Test that only global environment variables are merged with system env.""" @@ -50,9 +49,9 @@ def test_03_global_only(self) -> None: result = merge_environment_variables(global_env, None) # Assert - self.assertIsNotNone(result) - self.assertEqual(result["SYSTEM_VAR"], "system_value") - self.assertEqual(result["GLOBAL_VAR"], "global_value") + assert result is not None + assert result["SYSTEM_VAR"] == "system_value" + assert result["GLOBAL_VAR"] == "global_value" def test_04_check_only(self) -> None: """Test that only check-specific environment variables are merged with system env.""" @@ -64,9 +63,9 @@ def test_04_check_only(self) -> None: result = merge_environment_variables(None, check_env) # Assert - self.assertIsNotNone(result) - self.assertEqual(result["SYSTEM_VAR"], "system_value") - self.assertEqual(result["CHECK_VAR"], "check_value") + assert result is not None + assert result["SYSTEM_VAR"] == "system_value" + assert result["CHECK_VAR"] == "check_value" def test_05_check_overrides_global(self) -> None: """Test that check-specific variables override global variables.""" @@ -79,8 +78,8 @@ def test_05_check_overrides_global(self) -> None: result = merge_environment_variables(global_env, check_env) # Assert - self.assertIsNotNone(result) - self.assertEqual(result["API_KEY"], "check_key") + assert result is not None + assert result["API_KEY"] == "check_key" def test_06_global_overrides_system(self) -> None: """Test that global variables override system variables.""" @@ -92,8 +91,8 @@ def test_06_global_overrides_system(self) -> None: result = merge_environment_variables(global_env, None) # Assert - self.assertIsNotNone(result) - self.assertEqual(result["API_KEY"], "global_key") + assert result is not None + assert result["API_KEY"] == "global_key" def test_07_check_overrides_system(self) -> None: """Test that check-specific variables override system variables.""" @@ -105,8 +104,8 @@ def test_07_check_overrides_system(self) -> None: result = merge_environment_variables(None, check_env) # Assert - self.assertIsNotNone(result) - self.assertEqual(result["API_KEY"], "check_key") + assert result is not None + assert result["API_KEY"] == "check_key" def test_08_complete_merge_priority(self) -> None: """ @@ -134,13 +133,9 @@ def test_08_complete_merge_priority(self) -> None: result = merge_environment_variables(global_env, check_env) # Assert - self.assertIsNotNone(result) - self.assertEqual(result["VAR1"], "system_1") # System only - self.assertEqual(result["VAR2"], "global_2") # Global overrides system - self.assertEqual(result["VAR3"], "check_3") # Check overrides global and system - self.assertEqual(result["VAR4"], "global_4") # Global only - self.assertEqual(result["VAR5"], "check_5") # Check only - - -if __name__ == "__main__": - unittest.main() + assert result is not None + assert result["VAR1"] == "system_1" + assert result["VAR2"] == "global_2" + assert result["VAR3"] == "check_3" + assert result["VAR4"] == "global_4" + assert result["VAR5"] == "check_5" diff --git a/tests/unit/test_external_resources.py b/tests/unit/test_external_resources.py index eb79001..ffb4a10 100644 --- a/tests/unit/test_external_resources.py +++ b/tests/unit/test_external_resources.py @@ -2,43 +2,43 @@ import json import os -import unittest +from pathlib import Path from unittest.mock import MagicMock, patch +import pytest import requests from cove_sdk import BaseItem, JSONItem, PythonItem from cove_sdk.exceptions import CoveAPIError, URIParseError from grader.exceptions import ResourceError -from grader.utils.constants import TEMP_FILES_DIR from grader.utils.external_resources import Resource, ResourceType -class TestIsResourceRemote(unittest.TestCase): +class TestIsResourceRemote: """Unit tests for Resource._is_resource_remote.""" def test_01_scheme_http(self) -> None: """Test if the function correctly identifies a remote resource with HTTP scheme.""" - self.assertTrue(Resource._is_resource_remote("http://example.com/resource")) + assert Resource._is_resource_remote("http://example.com/resource") def test_02_scheme_https(self) -> None: """Test if the function correctly identifies a remote resource with HTTPS scheme.""" - self.assertTrue(Resource._is_resource_remote("https://example.com/resource")) + assert Resource._is_resource_remote("https://example.com/resource") def test_03_scheme_ftp(self) -> None: """Test if the function correctly identifies a remote resource with FTP scheme.""" - self.assertTrue(Resource._is_resource_remote("ftp://example.com/resource")) + assert Resource._is_resource_remote("ftp://example.com/resource") def test_04_scheme_file(self) -> None: """Test if the function correctly identifies a local resource with file scheme.""" - self.assertFalse(Resource._is_resource_remote("file:///path/to/resource")) + assert not Resource._is_resource_remote("file:///path/to/resource") def test_05_no_scheme(self) -> None: """Test if the function correctly identifies a local resource without a scheme.""" - self.assertFalse(Resource._is_resource_remote("/path/to/resource")) + assert not Resource._is_resource_remote("/path/to/resource") -class TestIsResourceCove(unittest.TestCase): +class TestIsResourceCove: """Unit tests for Resource._is_resource_cove.""" @patch("grader.utils.external_resources.is_cove_uri") @@ -48,33 +48,33 @@ def test_01_delegates_to_is_cove_uri(self, mock_is_cove_uri: MagicMock) -> None: result = Resource._is_resource_cove("cove://example/resource") - self.assertTrue(result) + assert result mock_is_cove_uri.assert_called_once_with("cove://example/resource") -class TestSniffResourceType(unittest.TestCase): +class TestSniffResourceType: """Unit tests for Resource._sniff_resource_type.""" @patch.object(Resource, "_is_resource_cove", return_value=True) @patch.object(Resource, "_is_resource_remote", return_value=False) def test_01_cove_takes_priority(self, _: MagicMock, __: MagicMock) -> None: """Test that a Cove URI is sniffed as COVE.""" - self.assertEqual(Resource._sniff_resource_type("cove://example/resource"), ResourceType.COVE) + assert Resource._sniff_resource_type("cove://example/resource") == ResourceType.COVE @patch.object(Resource, "_is_resource_cove", return_value=False) @patch.object(Resource, "_is_resource_remote", return_value=True) def test_02_remote(self, _: MagicMock, __: MagicMock) -> None: """Test that a remote URL is sniffed as REMOTE.""" - self.assertEqual(Resource._sniff_resource_type("http://example.com/resource"), ResourceType.REMOTE) + assert Resource._sniff_resource_type("http://example.com/resource") == ResourceType.REMOTE @patch.object(Resource, "_is_resource_cove", return_value=False) @patch.object(Resource, "_is_resource_remote", return_value=False) def test_03_local(self, _: MagicMock, __: MagicMock) -> None: """Test that a local path is sniffed as LOCAL.""" - self.assertEqual(Resource._sniff_resource_type("/path/to/resource"), ResourceType.LOCAL) + assert Resource._sniff_resource_type("/path/to/resource") == ResourceType.LOCAL -class TestReadLocalFile(unittest.TestCase): +class TestReadLocalFile: """Unit tests for Resource._read_local_file.""" @patch("pathlib.Path.read_text") @@ -84,23 +84,23 @@ def test_01_reads_file_contents_and_name(self, mock_read_text: MagicMock) -> Non content, name = Resource._read_local_file("/folderA/resource.txt") - self.assertEqual(content, "file contents") - self.assertEqual(name, "resource.txt") + assert content == "file contents" + assert name == "resource.txt" @patch("pathlib.Path.read_text", side_effect=FileNotFoundError) def test_02_missing_file_raises_resource_error(self, _: MagicMock) -> None: """Test that a missing file raises ResourceError.""" - with self.assertRaises(ResourceError): + with pytest.raises(ResourceError): Resource._read_local_file("/missing/resource.txt") @patch("pathlib.Path.read_text", side_effect=OSError) def test_03_os_error_raises_resource_error(self, _: MagicMock) -> None: """Test that an OSError while reading raises ResourceError.""" - with self.assertRaises(ResourceError): + with pytest.raises(ResourceError): Resource._read_local_file("/broken/resource.txt") -class TestDownloadFileFromUrl(unittest.TestCase): +class TestDownloadFileFromUrl: """Unit tests for Resource._download_file_from_url.""" @patch("requests.get") @@ -110,8 +110,8 @@ def test_01_returns_text_and_filename_from_url_path(self, mock_get: MagicMock) - content, filename = Resource._download_file_from_url("http://example.com/folderA/resource.txt") - self.assertEqual(content, "file contents") - self.assertEqual(filename, "resource.txt") + assert content == "file contents" + assert filename == "resource.txt" @patch("requests.get") def test_02_default_filename_when_url_has_no_path(self, mock_get: MagicMock) -> None: @@ -120,25 +120,25 @@ def test_02_default_filename_when_url_has_no_path(self, mock_get: MagicMock) -> _, filename = Resource._download_file_from_url("http://example.com") - self.assertEqual(filename, "downloaded_file") + assert filename == "downloaded_file" @patch("requests.get") def test_03_download_raises_resource_error_on_failure(self, mock_get: MagicMock) -> None: """Test that a failed request raises ResourceError.""" mock_get.side_effect = requests.RequestException("Download failed") - with self.assertRaises(ResourceError): + with pytest.raises(ResourceError): Resource._download_file_from_url("http://example.com/resource") -class TestFetchFromCove(unittest.TestCase): +class TestFetchFromCove: """Unit tests for Resource._fetch_from_cove.""" def test_01_missing_cove_api_key_raises_error(self) -> None: """Test that a missing COVE_API_KEY raises ResourceError.""" env_without_key = {k: v for k, v in os.environ.items() if k != "COVE_API_KEY"} - with patch.dict("os.environ", env_without_key, clear=True), self.assertRaises(ResourceError): + with patch.dict("os.environ", env_without_key, clear=True), pytest.raises(ResourceError): Resource._fetch_from_cove("cove://example/resource") @patch("grader.utils.external_resources.fetch_uri") @@ -146,7 +146,7 @@ def test_02_fetch_uri_raises_cove_api_error(self, mock_fetch: MagicMock) -> None """Test that a CoveAPIError from fetch_uri is wrapped in ResourceError.""" mock_fetch.side_effect = CoveAPIError("API error", detail="some detail") - with patch.dict("os.environ", {"COVE_API_KEY": "test_key"}), self.assertRaises(ResourceError): + with patch.dict("os.environ", {"COVE_API_KEY": "test_key"}), pytest.raises(ResourceError): Resource._fetch_from_cove("cove://example/resource") @patch("grader.utils.external_resources.fetch_uri") @@ -154,7 +154,7 @@ def test_03_fetch_uri_raises_uri_parse_error(self, mock_fetch: MagicMock) -> Non """Test that a URIParseError from fetch_uri is wrapped in ResourceError.""" mock_fetch.side_effect = URIParseError("Parse error") - with patch.dict("os.environ", {"COVE_API_KEY": "test_key"}), self.assertRaises(ResourceError): + with patch.dict("os.environ", {"COVE_API_KEY": "test_key"}), pytest.raises(ResourceError): Resource._fetch_from_cove("cove://example/resource") @patch("grader.utils.external_resources.fetch_uri") @@ -162,7 +162,7 @@ def test_04_fetch_uri_returns_none_raises_error(self, mock_fetch: MagicMock) -> """Test that None returned from fetch_uri raises ResourceError.""" mock_fetch.return_value = None - with patch.dict("os.environ", {"COVE_API_KEY": "test_key"}), self.assertRaises(ResourceError): + with patch.dict("os.environ", {"COVE_API_KEY": "test_key"}), pytest.raises(ResourceError): Resource._fetch_from_cove("cove://example/resource") @patch("grader.utils.external_resources.fetch_uri") @@ -177,8 +177,8 @@ def test_05_json_item_returns_dumped_json_and_filename(self, mock_fetch: MagicMo with patch.dict("os.environ", {"COVE_API_KEY": "test_key"}): content, filename = Resource._fetch_from_cove("cove://example/resource") - self.assertEqual(content, json.dumps(expected_json)) - self.assertEqual(filename, "my_config.json") + assert content == json.dumps(expected_json) + assert filename == "my_config.json" @patch("grader.utils.external_resources.fetch_uri") def test_06_python_item_returns_source_and_filename(self, mock_fetch: MagicMock) -> None: @@ -191,19 +191,19 @@ def test_06_python_item_returns_source_and_filename(self, mock_fetch: MagicMock) with patch.dict("os.environ", {"COVE_API_KEY": "test_key"}): content, filename = Resource._fetch_from_cove("cove://example/resource") - self.assertEqual(content, "print('hello')") - self.assertEqual(filename, "my_test.py") + assert content == "print('hello')" + assert filename == "my_test.py" @patch("grader.utils.external_resources.fetch_uri") def test_07_unknown_item_type_raises_error(self, mock_fetch: MagicMock) -> None: """Test that an unrecognized Cove item type raises ResourceError.""" mock_fetch.return_value = MagicMock(spec=BaseItem) - with patch.dict("os.environ", {"COVE_API_KEY": "test_key"}), self.assertRaises(ResourceError): + with patch.dict("os.environ", {"COVE_API_KEY": "test_key"}), pytest.raises(ResourceError): Resource._fetch_from_cove("cove://example/resource") -class TestResourceDownloadDispatch(unittest.TestCase): +class TestResourceDownloadDispatch: """Unit tests for Resource._download dispatching by resource type.""" @patch.object(Resource, "_fetch_from_cove", return_value=("content", "file.json")) @@ -211,7 +211,7 @@ def test_01_cove_dispatches_to_fetch_from_cove(self, mock_fetch: MagicMock) -> N """Test that COVE resources are downloaded via _fetch_from_cove.""" result = Resource._download("cove://example/resource", ResourceType.COVE) - self.assertEqual(result, ("content", "file.json")) + assert result == ("content", "file.json") mock_fetch.assert_called_once_with("cove://example/resource") @patch.object(Resource, "_download_file_from_url", return_value=("content", "file.txt")) @@ -219,7 +219,7 @@ def test_02_remote_dispatches_to_download_file_from_url(self, mock_download: Mag """Test that REMOTE resources are downloaded via _download_file_from_url.""" result = Resource._download("http://example.com/resource", ResourceType.REMOTE) - self.assertEqual(result, ("content", "file.txt")) + assert result == ("content", "file.txt") mock_download.assert_called_once_with("http://example.com/resource") @patch.object(Resource, "_read_local_file", return_value=("content", "file.txt")) @@ -227,11 +227,11 @@ def test_03_local_dispatches_to_read_local_file(self, mock_read: MagicMock) -> N """Test that LOCAL resources are read via _read_local_file.""" result = Resource._download("/path/to/resource", ResourceType.LOCAL) - self.assertEqual(result, ("content", "file.txt")) + assert result == ("content", "file.txt") mock_read.assert_called_once_with("/path/to/resource") -class TestResourceRead(unittest.TestCase): +class TestResourceRead: """Unit tests for Resource.read.""" @patch.object(Resource, "_download", return_value=("content", "file.txt")) @@ -239,29 +239,28 @@ def test_01_downloads_and_caches_content(self, mock_download: MagicMock) -> None """Test that read downloads once and caches the content for subsequent calls.""" resource = Resource("/path/file.txt") - self.assertEqual(resource.read(), "content") - self.assertEqual(resource.read(), "content") + assert resource.read() == "content" + assert resource.read() == "content" mock_download.assert_called_once() -class TestResourceToFile(unittest.TestCase): +class TestResourceToFile: """Unit tests for Resource.to_file.""" def test_01_local_resource_returns_source_path_unchanged(self) -> None: """Test that a local resource returns its source path without writing a file.""" resource = Resource("/path/to/file.txt") - self.assertEqual(resource.to_file(), "/path/to/file.txt") + assert resource.to_file() == "/path/to/file.txt" - @patch("builtins.open") @patch.object(Resource, "_download", return_value=("content", "downloaded.txt")) - def test_02_remote_resource_writes_content_to_temp_dir(self, _: MagicMock, mock_open: MagicMock) -> None: + def test_02_remote_resource_writes_content_to_temp_dir(self, _: MagicMock, tmp_path: Path) -> None: """Test that a non-local resource is written to TEMP_FILES_DIR and that path is returned.""" resource = Resource("http://example.com/downloaded.txt") - expected_path = os.path.join(TEMP_FILES_DIR, "downloaded.txt") + temp_dir = tmp_path / "temp_files" - result = resource.to_file() + with patch("grader.utils.external_resources.TEMP_FILES_DIR", temp_dir): + result = resource.to_file() - self.assertEqual(result, expected_path) - mock_open.assert_called_once_with(expected_path, "w+", encoding="utf-8") - mock_open.return_value.__enter__.return_value.write.assert_called_once_with("content") + assert result == str(temp_dir / "downloaded.txt") + assert (temp_dir / "downloaded.txt").read_text(encoding="utf-8") == "content" diff --git a/tests/unit/test_files.py b/tests/unit/test_files.py index b5ac6e9..52e80e5 100644 --- a/tests/unit/test_files.py +++ b/tests/unit/test_files.py @@ -1,11 +1,12 @@ """Unit tests for the file utility functions.""" -import os import shutil -import unittest import zipfile +from pathlib import Path from unittest.mock import MagicMock, patch +import pytest + from grader.utils.files import ( find_all_files_under_directory, find_all_python_files, @@ -16,18 +17,12 @@ ) -class TestFindAllPythonFiles(unittest.TestCase): +class TestFindAllPythonFiles: """Test cases for the find_all_python_files function.""" - def __init__(self, methodName: str = "runTest") -> None: - """ - Initialize the test case. - - :param methodName: The name of the test method to run. - :type methodName: str - """ + def setup_method(self) -> None: + """Set up the test environment.""" self.__sample_dir = "sample_dir" - super().__init__(methodName) @patch("grader.utils.structure_validator.StructureValidator.get_matching_files") def test_01_find_all_python_files(self, mocked_function: MagicMock) -> None: @@ -45,7 +40,7 @@ def test_01_find_all_python_files(self, mocked_function: MagicMock) -> None: actual_files = find_all_python_files(self.__sample_dir) # Assert - self.assertEqual(expected_files, actual_files) + assert expected_files == actual_files mocked_function.assert_called_once_with(self.__sample_dir) def __build_sample_files(self) -> tuple[list[str], list[str]]: @@ -56,48 +51,42 @@ def __build_sample_files(self) -> tuple[list[str], list[str]]: :rtype: tuple[list[str], list[str]] """ root_dir = self.__sample_dir - folder_1 = os.path.join(root_dir, "folder1") - folder_2 = os.path.join(root_dir, "folder2") - subfolder_2 = os.path.join(folder_2, "subfolder2") - subfolder_3 = os.path.join(folder_2, "subfolder3") - venv_dir_1 = os.path.join(root_dir, "venv") - venv_dir_2 = os.path.join(root_dir, ".venv") + folder_1 = str(Path(root_dir, "folder1")) + folder_2 = str(Path(root_dir, "folder2")) + subfolder_2 = str(Path(folder_2, "subfolder2")) + subfolder_3 = str(Path(folder_2, "subfolder3")) + venv_dir_1 = str(Path(root_dir, "venv")) + venv_dir_2 = str(Path(root_dir, ".venv")) all_files = [ - os.path.join(folder_1, "file2.py"), - os.path.join(folder_1, "file3.py"), - os.path.join(folder_2, "file5.py"), - os.path.join(subfolder_2, "file9.py"), - os.path.join(subfolder_3, "file10.py"), - os.path.join(venv_dir_1, "file11.py"), - os.path.join(venv_dir_1, "file12.py"), - os.path.join(venv_dir_2, "file13.py"), - os.path.join(venv_dir_2, "file14.py"), + str(Path(folder_1, "file2.py")), + str(Path(folder_1, "file3.py")), + str(Path(folder_2, "file5.py")), + str(Path(subfolder_2, "file9.py")), + str(Path(subfolder_3, "file10.py")), + str(Path(venv_dir_1, "file11.py")), + str(Path(venv_dir_1, "file12.py")), + str(Path(venv_dir_2, "file13.py")), + str(Path(venv_dir_2, "file14.py")), ] expected_files = [ - os.path.join(folder_1, "file2.py"), - os.path.join(folder_1, "file3.py"), - os.path.join(folder_2, "file5.py"), - os.path.join(subfolder_2, "file9.py"), - os.path.join(subfolder_3, "file10.py"), + str(Path(folder_1, "file2.py")), + str(Path(folder_1, "file3.py")), + str(Path(folder_2, "file5.py")), + str(Path(subfolder_2, "file9.py")), + str(Path(subfolder_3, "file10.py")), ] return all_files, expected_files -class TestFindAllSourceFiles(unittest.TestCase): +class TestFindAllSourceFiles: """Test cases for the find_all_source_files function.""" - def __init__(self, methodName: str = "runTest") -> None: - """ - Initialize the test case. - - :param methodName: The name of the test method to run. - :type methodName: str - """ + def setup_method(self) -> None: + """Set up the test environment.""" self.__sample_dir = "sample_dir" - super().__init__(methodName) @patch("grader.utils.files.find_all_test_files") @patch("grader.utils.files.find_all_python_files") @@ -120,12 +109,10 @@ def test_01_find_all_source_files( """ # Arrange python_file_names = [f"file{i}" for i in range(1, 11)] - python_file_paths = [ - os.path.join(self.__sample_dir, python_file_name) for python_file_name in python_file_names - ] + python_file_paths = [str(Path(self.__sample_dir, python_file_name)) for python_file_name in python_file_names] test_file_names = [f"file{i}" for i in range(1, 11, 3)] - test_file_paths = [os.path.join(self.__sample_dir, test_file_name) for test_file_name in test_file_names] + test_file_paths = [str(Path(self.__sample_dir, test_file_name)) for test_file_name in test_file_names] mocked_all_python_files.return_value = python_file_paths mocked_find_all_test_files.return_value = test_file_paths @@ -137,24 +124,18 @@ def test_01_find_all_source_files( actual_files = find_all_source_files(self.__sample_dir) # Assert - self.assertEqual(sorted(expected_files), sorted(actual_files)) + assert sorted(expected_files) == sorted(actual_files) mocked_all_python_files.assert_called_once_with(self.__sample_dir) mocked_find_all_test_files.assert_called_once_with(self.__sample_dir) mocked_tests_directory_name.assert_called_once_with(self.__sample_dir) -class TestFindAllTestFiles(unittest.TestCase): +class TestFindAllTestFiles: """Test cases for the find_all_test_files function.""" - def __init__(self, methodName: str = "runTest") -> None: - """ - Initialize the test case. - - :param methodName: The name of the test method to run. - :type methodName: str - """ + def setup_method(self) -> None: + """Set up the test environment.""" self.__sample_dir = "sample_dir" - super().__init__(methodName) @patch("grader.utils.files.find_all_files_under_directory") def test_01_no_directory_provided(self, mocked_find_all_files_under_directory: MagicMock) -> None: @@ -171,7 +152,7 @@ def test_01_no_directory_provided(self, mocked_find_all_files_under_directory: M actual_value = find_all_test_files(tests_directory=None) # Assert - self.assertEqual(expected_value, actual_value) + assert expected_value == actual_value mocked_find_all_files_under_directory.assert_not_called() @patch("grader.utils.structure_validator.StructureValidator.get_matching_files") @@ -184,8 +165,8 @@ def test_02_directory_provided(self, mocked_find_all_files_under_directory: Magi """ # Arrange expected_value: list[str] = [ - os.path.join(self.__sample_dir, "file1.py"), - os.path.join(self.__sample_dir, "file2.py"), + str(Path(self.__sample_dir, "file1.py")), + str(Path(self.__sample_dir, "file2.py")), ] mocked_find_all_files_under_directory.return_value = expected_value @@ -194,67 +175,60 @@ def test_02_directory_provided(self, mocked_find_all_files_under_directory: Magi actual_value = find_all_test_files(self.__sample_dir) # Assert - self.assertEqual(expected_value, actual_value) + assert expected_value == actual_value mocked_find_all_files_under_directory.assert_called_once_with(self.__sample_dir) -class TestGetTestsDirectoryName(unittest.TestCase): +class TestGetTestsDirectoryName: """Test cases for the get_tests_directory_name function.""" - def __init__(self, methodName: str = "runTest") -> None: - """ - Initialize the test case. - - :param methodName: The name of the test method to run. - :type methodName: str - """ + def setup_method(self) -> None: + """Set up the test environment.""" self.__sample_dir = "sample_dir" - super().__init__(methodName) - def tearDown(self) -> None: + def teardown_method(self) -> None: """Tear down the test environment.""" - if os.path.exists(self.__sample_dir): + if Path(self.__sample_dir).exists(): shutil.rmtree(self.__sample_dir) - return super().tearDown() def test_01_tests_directory(self) -> None: """Verify that get_tests_directory_name returns the proper directory name.""" # Arrange test_dir_name = "tests" - expected_test_dir = os.path.join(self.__sample_dir, test_dir_name) + expected_test_dir = str(Path(self.__sample_dir, test_dir_name)) self.__create_structure(self.__sample_dir, test_dir_name) # Act actual_test_dir = get_tests_directory_name(self.__sample_dir) # Assert - self.assertEqual(expected_test_dir, actual_test_dir) + assert expected_test_dir == actual_test_dir def test_02_test_directory(self) -> None: """Verify that get_tests_directory_name returns the proper directory name.""" # Arrange test_dir_name = "test" - expected_test_dir = os.path.join(self.__sample_dir, test_dir_name) + expected_test_dir = str(Path(self.__sample_dir, test_dir_name)) self.__create_structure(self.__sample_dir, test_dir_name) # Act actual_test_dir = get_tests_directory_name(self.__sample_dir) # Assert - self.assertEqual(expected_test_dir, actual_test_dir) + assert expected_test_dir == actual_test_dir def test_03_tst_directory(self) -> None: """Verify that get_tests_directory_name returns the proper directory name.""" # Arrange test_dir_name = "tst" - expected_test_dir = os.path.join(self.__sample_dir, test_dir_name) + expected_test_dir = str(Path(self.__sample_dir, test_dir_name)) self.__create_structure(self.__sample_dir, test_dir_name) # Act actual_test_dir = get_tests_directory_name(self.__sample_dir) # Assert - self.assertEqual(expected_test_dir, actual_test_dir) + assert expected_test_dir == actual_test_dir def test_04_no_directory(self) -> None: """Verify that get_tests_directory_name returns None when there is no tests directory.""" @@ -266,7 +240,7 @@ def test_04_no_directory(self) -> None: actual_test_dir = get_tests_directory_name(self.__sample_dir) # Assert - self.assertIsNone(actual_test_dir) + assert actual_test_dir is None @staticmethod def __create_structure(root_dir: str, tests_dir: str) -> None: @@ -278,151 +252,53 @@ def __create_structure(root_dir: str, tests_dir: str) -> None: :param tests_dir: The tests directory. :type tests_dir: str """ - full_path = os.path.join(root_dir, tests_dir) - os.makedirs(full_path) + full_path = str(Path(root_dir, tests_dir)) + Path(full_path).mkdir(parents=True) -class TestFindAllFilesUnderDirectory(unittest.TestCase): +class TestFindAllFilesUnderDirectory: """Test cases for the find_all_files_under_directory function.""" - type DirectoryStructure = list[tuple[str, list[str], list[str]]] - - def __init__(self, methodName: str = "runTest") -> None: - """ - Initialize the test case. - - :param methodName: The name of the test method to run. - :type methodName: str - """ - self.__sample_dir = "sample_dir" - super().__init__(methodName) - - @patch("os.walk") - def test_01_partial_files_result(self, mocked_os_walk: MagicMock) -> None: - """ - Verify that find_all_files_under_directory returns the proper files. - - Tests with multiple files with different extensions. - - :param mocked_os_walk: Mocked os.walk function. - :type mocked_os_walk: MagicMock - """ - files = [ - ["file1.txt", "file2.csv"], - ["file3.txt"], - ["file4.csv", "file5.txt"], - ["file6.csv"], - ] - mocked_files = self.__build_sample_files(files) - - self.__base_test(mocked_os_walk, mocked_files, extension_to_search_for=".txt") - - @patch("os.walk") - def test_02_all_files_result(self, mocked_os_walk: MagicMock) -> None: - """ - Verify that find_all_files_under_directory returns the proper files. - - Tests with multiple files with the same extension. + @staticmethod + def __build_tree(root: Path, files: list[str]) -> None: + for file in files: + (root / file).parent.mkdir(parents=True, exist_ok=True) + (root / file).touch() - :param mocked_os_walk: Mocked os.walk function. - :type mocked_os_walk: MagicMock - """ - files = [ - ["file1.txt", "file2.txt"], - ["file3.txt"], - ["file4.txt", "file5.txt"], - ["file6.txt"], - ] - mocked_files = self.__build_sample_files(files) + def test_01_partial_files_result(self, tmp_path: Path) -> None: + """Verify that only files with the requested extension are returned, recursively.""" + self.__build_tree( + tmp_path, ["file1.txt", "file2.csv", "dir/file3.txt", "dir/sub/file4.csv", "dir/sub/file5.txt"] + ) + expected = sorted(str(tmp_path / name) for name in ["file1.txt", "dir/file3.txt", "dir/sub/file5.txt"]) - self.__base_test(mocked_os_walk, mocked_files, extension_to_search_for=".txt") + assert sorted(find_all_files_under_directory(str(tmp_path), ".txt")) == expected - @patch("os.walk") - def test_03_no_files_result(self, mocked_os_walk: MagicMock) -> None: - """ - Verify that find_all_files_under_directory returns an empty list. + def test_02_all_files_result(self, tmp_path: Path) -> None: + """Verify that all files are returned when they all have the requested extension.""" + names = ["file1.txt", "file2.txt", "dir/file3.txt", "dir/sub/file4.txt"] + self.__build_tree(tmp_path, names) - Tests when there are no files with the searched extension. + assert sorted(find_all_files_under_directory(str(tmp_path), ".txt")) == sorted(str(tmp_path / n) for n in names) - :param mocked_os_walk: Mocked os.walk function. - :type mocked_os_walk: MagicMock - """ - # Arrange - files = [ - ["file1.txt", "file2.csv"], - ["file3.txt"], - ["file4.csv", "file5.txt"], - ["file6.csv"], - ] - mocked_files = self.__build_sample_files(files) + def test_03_no_files_result(self, tmp_path: Path) -> None: + """Verify that an empty list is returned when no file has the requested extension.""" + self.__build_tree(tmp_path, ["file1.csv", "dir/file2.csv"]) - self.__base_test(mocked_os_walk, mocked_files, extension_to_search_for=".py") + assert not find_all_files_under_directory(str(tmp_path), ".py") - @patch("os.walk") - def test_03_no_files_overall(self, mocked_os_walk: MagicMock) -> None: - """ - Verify that find_all_files_under_directory returns an empty list. + def test_04_no_files_overall(self, tmp_path: Path) -> None: + """Verify that an empty list is returned for an empty directory.""" + assert not find_all_files_under_directory(str(tmp_path), ".py") - Tests when there are no files. - - :param mocked_os_walk: Mocked os.walk function. - :type mocked_os_walk: MagicMock - """ - self.__base_test(mocked_os_walk, mocked_files=[], extension_to_search_for=".py") - - def __base_test( - self, - mocked_os_walk: MagicMock, - mocked_files: DirectoryStructure, - extension_to_search_for: str, - ) -> None: - """ - Provide base test for find_all_files_under_directory. - - :param mocked_os_walk: Mocked os.walk function. - :type mocked_os_walk: MagicMock - :param mocked_files: Mocked directory structure. - :type mocked_files: DirectoryStructure - :param extension_to_search_for: The file extension to search for. - :type extension_to_search_for: str - """ - # Arrange - mocked_os_walk.return_value = mocked_files - - expected_files = [ - os.path.join(root, file) - for root, _, files in mocked_os_walk.return_value - for file in files - if file.endswith(extension_to_search_for) - ] - - # Act - actual_files = find_all_files_under_directory(self.__sample_dir, extension_to_search_for) - - # Assert - self.assertEqual(expected_files, actual_files) - - def __build_sample_files(self, files: list[list[str]]) -> DirectoryStructure: - """ - Build the sample files for the test. - - :param files: The list of files in each directory. - :type files: list[list[str]] - :return: The directory structure. - :rtype: DirectoryStructure - """ - root_dirs = [ - os.path.join(os.sep, self.__sample_dir), - os.path.join(os.sep, self.__sample_dir, "folder1"), - os.path.join(os.sep, self.__sample_dir, "folder1", "subfolder1"), - os.path.join(os.sep, self.__sample_dir, "folder2"), - ] - subdirs = [["folder1", "folder2"], ["subfolder1"], []] + def test_05_directory_matching_extension_is_skipped(self, tmp_path: Path) -> None: + """Verify that a directory whose name ends with the extension is not returned.""" + (tmp_path / "package.py").mkdir() - return list(zip(root_dirs, subdirs, files)) + assert not find_all_files_under_directory(str(tmp_path), ".py") -class TestUnzipArchive(unittest.TestCase): +class TestUnzipArchive: """Test cases for the unzip_archive function.""" @patch("zipfile.ZipFile") @@ -438,7 +314,7 @@ def test_01_unzip_to_default_directory(self, mock_zipfile: MagicMock) -> None: # Assert mock_zip.extractall.assert_called_once() - self.assertIn("test_archive", result_dir) + assert "test_archive" in result_dir @patch("zipfile.ZipFile") def test_02_unzip_to_custom_directory(self, mock_zipfile: MagicMock) -> None: @@ -451,18 +327,18 @@ def test_02_unzip_to_custom_directory(self, mock_zipfile: MagicMock) -> None: result_dir = unzip_archive(archive_path, target_directory=custom_dir) # Assert mock_zip.extractall.assert_called_once_with(custom_dir) - self.assertEqual(result_dir, custom_dir) + assert result_dir == custom_dir @patch("zipfile.ZipFile", side_effect=zipfile.BadZipFile) def test_03_invalid_zip_file(self, mock_exists: MagicMock) -> None: """Verify that unzip_archive raises an exception for an invalid zip file.""" archive_path = "/fake/path/invalid.zip" - with self.assertRaises(zipfile.BadZipFile): + with pytest.raises(zipfile.BadZipFile): unzip_archive(archive_path) - self.assertTrue(mock_exists.called) + assert mock_exists.called def test_04_nonexistent_zip_file(self) -> None: """Verify that unzip_archive raises an exception for a nonexistent file.""" archive_path = "/nonexistent/path/to/file.zip" - with self.assertRaises(FileNotFoundError): + with pytest.raises(FileNotFoundError): unzip_archive(archive_path) diff --git a/tests/unit/test_grader.py b/tests/unit/test_grader.py index e488285..d69d503 100644 --- a/tests/unit/test_grader.py +++ b/tests/unit/test_grader.py @@ -1,10 +1,11 @@ """Unit tests for the Grader class.""" -import os import tempfile -import unittest +from pathlib import Path from unittest.mock import ANY, MagicMock, patch +import pytest + import grader.utils.constants as const from grader.checks.abstract_check import NonScoredCheck, ScoredCheck from grader.exceptions import CheckError, InvalidCheckError, InvalidConfigError, InvalidProjectRootError @@ -12,7 +13,7 @@ from grader.models.check_result import NonScoredCheckResult, ScoredCheckResult -class TestGrader(unittest.TestCase): +class TestGrader: """Unit tests for the Grader class.""" @patch("grader.utils.config.load_config") @@ -22,26 +23,26 @@ def test_01_load_config_fails(self, mock_load_config: MagicMock) -> None: mock_load_config.side_effect = FileNotFoundError("Config file not found") # Act - with self.assertRaises(InvalidConfigError): + with pytest.raises(InvalidConfigError): Grader(config_path="config_path", logger=MagicMock()) - @patch("os.path.exists") + @patch("pathlib.Path.exists") def test_02_project_root_does_not_exist(self, mock_exists: MagicMock) -> None: """Test that Grader exits with SystemExit when the project root directory does not exist.""" # Arrange mock_exists.return_value = False - config_path = os.path.join("config", "full_single_point.json") + config_path = str(Path("config", "full_single_point.json")) grader = Grader(config_path=config_path, logger=MagicMock()) # Act & Assert - with self.assertRaises(InvalidProjectRootError): + with pytest.raises(InvalidProjectRootError): grader.grade("nonexistent_project_root", "student_id") @patch("grader.grader.create_checks") def test_03_create_checks_is_called(self, mock_create_checks: MagicMock) -> None: """Test that create_checks is called when grade() is called.""" # Arrange - sample_config_path = os.path.join("config", "full_single_point.json") + sample_config_path = str(Path("config", "full_single_point.json")) mock_create_checks.return_value = ([], []) grader = Grader(config_path=sample_config_path, logger=MagicMock()) @@ -57,7 +58,7 @@ def test_03_create_checks_is_called(self, mock_create_checks: MagicMock) -> None def test_04_non_venv_checks_run_called(self, mock_create_checks: MagicMock) -> None: """Test that the run method of each non_venv_check is called when grade() is executed.""" # Arrange - sample_config_path = os.path.join("config", "full_single_point.json") + sample_config_path = str(Path("config", "full_single_point.json")) mock_check1 = MagicMock() mock_check2 = MagicMock() @@ -79,7 +80,7 @@ def test_04_non_venv_checks_run_called(self, mock_create_checks: MagicMock) -> N def test_05_non_venv_checks_results_are_returned(self, mock_create_checks: MagicMock) -> None: """Test that the results from non_venv_checks are returned by grade().""" # Arrange - sample_config_path = os.path.join("config", "full_single_point.json") + sample_config_path = str(Path("config", "full_single_point.json")) mock_check1 = MagicMock() mock_check2 = MagicMock() @@ -96,7 +97,7 @@ def test_05_non_venv_checks_results_are_returned(self, mock_create_checks: Magic results = grader.grade(sample_project_path, "student_id") # Assert - self.assertEqual(results.results, [result1, result2]) + assert results.results == [result1, result2] @patch("grader.grader.create_checks") def test_06_skipping_venv_creation_returns_only_non_venv_checks(self, mock_create_checks: MagicMock) -> None: @@ -106,7 +107,7 @@ def test_06_skipping_venv_creation_returns_only_non_venv_checks(self, mock_creat Only non_venv_checks results are returned by grade(). """ # Arrange - sample_config_path = os.path.join("config", "full_single_point.json") + sample_config_path = str(Path("config", "full_single_point.json")) mock_non_venv_check1 = MagicMock() mock_non_venv_check2 = MagicMock() @@ -134,7 +135,7 @@ def test_06_skipping_venv_creation_returns_only_non_venv_checks(self, mock_creat results = grader.grade(sample_project_path, "student_id") # Assert - self.assertEqual(results.results, [result1, result2]) + assert results.results == [result1, result2] @patch("grader.grader.VirtualEnvironment") @patch("grader.grader.create_checks") @@ -143,7 +144,7 @@ def test_07_venv_checks_run_called_in_context_manager( ) -> None: """Test that the run method of each venv_check is called inside the VirtualEnvironment context manager.""" # Arrange - sample_config_path = os.path.join("config", "full_single_point.json") + sample_config_path = str(Path("config", "full_single_point.json")) mock_non_venv_check = MagicMock() mock_venv_check1 = MagicMock() @@ -175,7 +176,7 @@ def test_08_venv_checks_results_are_returned( ) -> None: """Test that the results from venv_checks are returned by grade().""" # Arrange - sample_config_path = os.path.join("config", "full_single_point.json") + sample_config_path = str(Path("config", "full_single_point.json")) mock_non_venv_check = MagicMock() mock_venv_check1 = MagicMock() @@ -199,14 +200,14 @@ def test_08_venv_checks_results_are_returned( results = grader.grade(sample_project_path, "student_id") # Assert - self.assertEqual(results.results, [non_venv_result, venv_result1, venv_result2]) + assert results.results == [non_venv_result, venv_result1, venv_result2] @patch("grader.grader.create_checks") def test_09_scored_checkerror_returns_scored_result(self, mock_create_checks: MagicMock) -> None: """Test that if a ScoredCheck raises CheckError, grade returns a ScoredCheckResult with score 0.""" # Arrange - sample_config_path = os.path.join("config", "full_single_point.json") + sample_config_path = str(Path("config", "full_single_point.json")) mock_scored_check = MagicMock(spec=ScoredCheck) mock_scored_check.name = "scored_check" @@ -221,16 +222,16 @@ def test_09_scored_checkerror_returns_scored_result(self, mock_create_checks: Ma results = grader.grade(sample_project_path, "student_id") # Assert - self.assertEqual(len(results.results), 1) - self.assertIsInstance(results.results[0], ScoredCheckResult) - self.assertEqual(results.results[0].name, "scored_check") - self.assertEqual(results.results[0].result, 0) - self.assertEqual(results.results[0].max_score, 10) # type: ignore + assert len(results.results) == 1 + assert isinstance(results.results[0], ScoredCheckResult) + assert results.results[0].name == "scored_check" + assert results.results[0].result == 0 + assert results.results[0].max_score == 10 @patch("grader.grader.create_checks") def test_10_nonscored_checkerror_returns_nonscored_result(self, mock_create_checks: MagicMock) -> None: """Test that if a NonScoredCheck raises CheckError, grade returns a NonScoredCheckResult with result=False.""" - sample_config_path = os.path.join("config", "full_single_point.json") + sample_config_path = str(Path("config", "full_single_point.json")) mock_nonscored_check = MagicMock(spec=NonScoredCheck) mock_nonscored_check.name = "nonscored_check" @@ -244,15 +245,15 @@ def test_10_nonscored_checkerror_returns_nonscored_result(self, mock_create_chec results = grader.grade(sample_project_path, "student_id") # Assert - self.assertEqual(len(results.results), 1) - self.assertIsInstance(results.results[0], NonScoredCheckResult) - self.assertEqual(results.results[0].name, "nonscored_check") - self.assertFalse(results.results[0].result) + assert len(results.results) == 1 + assert isinstance(results.results[0], NonScoredCheckResult) + assert results.results[0].name == "nonscored_check" + assert not results.results[0].result @patch("grader.grader.create_checks") def test_11_unknown_checkerror_raises_typeerror(self, mock_create_checks: MagicMock) -> None: """Test that if a check is neither ScoredCheck nor NonScoredCheck and raises CheckError, grade raises TypeError.""" - sample_config_path = os.path.join("config", "full_single_point.json") + sample_config_path = str(Path("config", "full_single_point.json")) class UnknownCheck: """Dummy check for the test.""" @@ -267,14 +268,14 @@ def run(self) -> None: grader = Grader(config_path=sample_config_path, logger=MagicMock()) # Act & Assert - with tempfile.TemporaryDirectory() as sample_project_path, self.assertRaises(TypeError): + with tempfile.TemporaryDirectory() as sample_project_path, pytest.raises(TypeError): grader.grade(sample_project_path, "student_id") @patch("grader.grader.create_checks") def test_12_one_grader_grades_multiple_projects(self, mock_create_checks: MagicMock) -> None: """Test that a single Grader instance can grade multiple project roots via separate grade() calls.""" # Arrange - sample_config_path = os.path.join("config", "full_single_point.json") + sample_config_path = str(Path("config", "full_single_point.json")) mock_create_checks.return_value = ([], []) grader = Grader(config_path=sample_config_path, logger=MagicMock()) @@ -285,28 +286,28 @@ def test_12_one_grader_grades_multiple_projects(self, mock_create_checks: MagicM grader.grade(second_project_path, "student_2") # Assert - self.assertEqual(mock_create_checks.call_count, 2) + assert mock_create_checks.call_count == 2 graded_project_roots = [call.args[1] for call in mock_create_checks.call_args_list] - self.assertEqual(graded_project_roots, [first_project_path, second_project_path]) + assert graded_project_roots == [first_project_path, second_project_path] def test_13_missing_config_path_raises(self) -> None: """Test that Grader raises InvalidConfigError when no config_path is provided at all.""" # Act & Assert - with self.assertRaises(InvalidConfigError): + with pytest.raises(InvalidConfigError): Grader(config_path=None, logger=MagicMock()) @patch("grader.grader.create_checks") def test_14_run_checks_error_is_logged_and_reraised(self, mock_create_checks: MagicMock) -> None: """Test that an InvalidCheckError from __run_checks is logged and re-raised by grade().""" # Arrange - sample_config_path = os.path.join("config", "full_single_point.json") + sample_config_path = str(Path("config", "full_single_point.json")) mock_create_checks.side_effect = InvalidCheckError("Unknown check name: bogus") mock_logger = MagicMock() grader = Grader(config_path=sample_config_path, logger=mock_logger) # Act & Assert - with tempfile.TemporaryDirectory() as sample_project_path, self.assertRaises(InvalidCheckError): + with tempfile.TemporaryDirectory() as sample_project_path, pytest.raises(InvalidCheckError): grader.grade(sample_project_path, "student_id") mock_logger.error.assert_called_once() @@ -316,27 +317,27 @@ def test_14_run_checks_error_is_logged_and_reraised(self, mock_create_checks: Ma def test_15_cleanup_removes_existing_coverage_file(self, mock_create_checks: MagicMock) -> None: """Test that grade() removes a leftover coverage file from the project root after running.""" # Arrange - sample_config_path = os.path.join("config", "full_single_point.json") + sample_config_path = str(Path("config", "full_single_point.json")) mock_create_checks.return_value = ([], []) grader = Grader(config_path=sample_config_path, logger=MagicMock()) # Act with tempfile.TemporaryDirectory() as sample_project_path: - coverage_file_path = os.path.join(sample_project_path, const.COVERAGE_FILE) + coverage_file_path = str(Path(sample_project_path, const.COVERAGE_FILE)) with open(coverage_file_path, "w", encoding="utf-8"): pass grader.grade(sample_project_path, "student_id") # Assert - self.assertFalse(os.path.exists(coverage_file_path)) + assert not Path(coverage_file_path).exists() @patch("grader.grader.create_checks") def test_16_selected_checks_forwarded_to_create_checks(self, mock_create_checks: MagicMock) -> None: """Test that selected_checks passed to Grader is forwarded to create_checks.""" # Arrange - sample_config_path = os.path.join("config", "full_single_point.json") + sample_config_path = str(Path("config", "full_single_point.json")) mock_create_checks.return_value = ([], []) grader = Grader(config_path=sample_config_path, logger=MagicMock(), selected_checks=["pylint", "coverage"]) @@ -352,7 +353,7 @@ def test_16_selected_checks_forwarded_to_create_checks(self, mock_create_checks: def test_17_selected_checks_defaults_to_none(self, mock_create_checks: MagicMock) -> None: """Test that create_checks receives selected_checks=None when Grader is not given a selection.""" # Arrange - sample_config_path = os.path.join("config", "full_single_point.json") + sample_config_path = str(Path("config", "full_single_point.json")) mock_create_checks.return_value = ([], []) grader = Grader(config_path=sample_config_path, logger=MagicMock()) diff --git a/tests/unit/test_json_with_templates.py b/tests/unit/test_json_with_templates.py index 1108212..f67f5c4 100644 --- a/tests/unit/test_json_with_templates.py +++ b/tests/unit/test_json_with_templates.py @@ -1,12 +1,13 @@ """Tests for JSON template utilities.""" -import unittest from unittest.mock import MagicMock, mock_open, patch +import pytest + from grader.utils.json_with_templates import load_with_values -class TestJsonWithTemplates(unittest.TestCase): +class TestJsonWithTemplates: """Tests for the load_with_values function.""" @patch("builtins.open", new_callable=mock_open, read_data='{"key": "${{value}}"}') @@ -19,7 +20,7 @@ def test_01_successful_substitution(self, _: MagicMock) -> None: result = load_with_values("dummy.json", value="test") # Assert - self.assertEqual(result, {"key": "test"}) + assert result == {"key": "test"} @patch("builtins.open", new_callable=mock_open, read_data='{"key": "${{missing}}"}') def test_02_missing_placeholder_value(self, _: MagicMock) -> None: @@ -28,9 +29,9 @@ def test_02_missing_placeholder_value(self, _: MagicMock) -> None: # (mock_file already set up) # Act / Assert - with self.assertRaises(ValueError) as ctx: + with pytest.raises(ValueError) as ctx: load_with_values("dummy.json", not_missing="test") - self.assertIn("Missing value for placeholder: missing", str(ctx.exception)) + assert "Missing value for placeholder: missing" in str(ctx.value) @patch("builtins.open", new_callable=mock_open, read_data='{"a": "${{x}}", "b": "${{y}}"}') def test_04_multiple_placeholders(self, _: MagicMock) -> None: @@ -42,7 +43,7 @@ def test_04_multiple_placeholders(self, _: MagicMock) -> None: result = load_with_values("dummy.json", x="foo", y="bar") # Assert - self.assertEqual(result, {"a": "foo", "b": "bar"}) + assert result == {"a": "foo", "b": "bar"} @patch("builtins.open", new_callable=mock_open, read_data='{"num": "${{n}}"}') def test_05_non_string_value(self, _: MagicMock) -> None: @@ -51,4 +52,4 @@ def test_05_non_string_value(self, _: MagicMock) -> None: result = load_with_values("dummy.json", n=123) # type: ignore # Assert - self.assertEqual(result, {"num": "123"}) + assert result == {"num": "123"} diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 9be1af8..7f52d41 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -1,9 +1,8 @@ """Unit tests for the main module.""" -import os import tempfile -import unittest import zipfile +from pathlib import Path from unittest.mock import MagicMock, call, patch import grader.utils.constants as const @@ -13,7 +12,7 @@ from grader.utils.results_reporter import CSVResultsReporter, JSONResultsReporter, PlainTextResultsReporter -class TestBuildReporter(unittest.TestCase): +class TestBuildReporter: """Tests for the build_reporter function.""" def test_01_build_json_reporter(self) -> None: @@ -25,7 +24,7 @@ def test_01_build_json_reporter(self) -> None: reporter = build_reporter(reporter_format, is_verbose=False) # Assert - self.assertIsInstance(reporter, JSONResultsReporter) + assert isinstance(reporter, JSONResultsReporter) def test_02_build_csv_reporter(self) -> None: """Test if the function returns a CSVResultsReporter when "csv" is passed.""" @@ -36,7 +35,7 @@ def test_02_build_csv_reporter(self) -> None: reporter = build_reporter(reporter_format, is_verbose=False) # Assert - self.assertIsInstance(reporter, CSVResultsReporter) + assert isinstance(reporter, CSVResultsReporter) def test_03_build_plaintext_reporter(self) -> None: """Test if the function returns a PlainTextResultsReporter when "text" is passed.""" @@ -47,7 +46,7 @@ def test_03_build_plaintext_reporter(self) -> None: reporter = build_reporter(reporter_format, is_verbose=False) # Assert - self.assertIsInstance(reporter, PlainTextResultsReporter) + assert isinstance(reporter, PlainTextResultsReporter) def test_04_build_default_reporter(self) -> None: """Test if the function returns a PlainTextResultsReporter when an unknown format is passed.""" @@ -58,10 +57,10 @@ def test_04_build_default_reporter(self) -> None: reporter = build_reporter(reporter_format, is_verbose=False) # Assert - self.assertIsInstance(reporter, PlainTextResultsReporter) + assert isinstance(reporter, PlainTextResultsReporter) -class TestRunGrader(unittest.TestCase): +class TestRunGrader: """Tests for the run_grader function.""" @patch("desktop.main.get_args") @@ -112,7 +111,7 @@ def test_02_is_suppressing_info_json(self, mock_setup_logger: MagicMock, mock_ge actual_suppress_info = mock_setup_logger.call_args_list[0].kwargs["suppress_info"] # Assert - self.assertEqual(actual_suppress_info, expected_suppress_info) + assert actual_suppress_info == expected_suppress_info @patch("desktop.main.get_args") @patch("desktop.main.setup_logger") @@ -139,7 +138,7 @@ def test_03_is_suppressing_info_csv(self, mock_setup_logger: MagicMock, mock_get actual_suppress_info = mock_setup_logger.call_args_list[0].kwargs["suppress_info"] # Assert - self.assertEqual(actual_suppress_info, expected_suppress_info) + assert actual_suppress_info == expected_suppress_info @patch("desktop.main.get_args") @patch("desktop.main.setup_logger") @@ -166,7 +165,7 @@ def test_04_is_suppressing_info_passed_true(self, mock_setup_logger: MagicMock, actual_suppress_info = mock_setup_logger.call_args_list[0].kwargs["suppress_info"] # Assert - self.assertEqual(actual_suppress_info, expected_suppress_info) + assert actual_suppress_info == expected_suppress_info @patch("desktop.main.get_args") @patch("desktop.main.setup_logger") @@ -193,7 +192,7 @@ def test_04_is_suppressing_info_not_passed(self, mock_setup_logger: MagicMock, m actual_suppress_info = mock_setup_logger.call_args_list[0].kwargs["suppress_info"] # Assert - self.assertEqual(actual_suppress_info, expected_suppress_info) + assert actual_suppress_info == expected_suppress_info @patch("desktop.main.get_args") @patch("desktop.main.setup_logger") @@ -330,7 +329,7 @@ def test_08_results_reporter_called( @patch("desktop.main.get_args") @patch("desktop.main.Grader") @patch("desktop.main.setup_logger") - @patch("desktop.main.extract_student_id_from_path", side_effect=os.path.basename) + @patch("desktop.main.extract_student_id_from_path", side_effect=lambda p: Path(p).name) def test_09_glob_project_root_grades_each_match( self, _mock_extract_student_id: MagicMock, @@ -341,14 +340,14 @@ def test_09_glob_project_root_grades_each_match( """Test that a glob project_root grades every matched directory, using its name as the run id.""" # Arrange with tempfile.TemporaryDirectory() as batch_dir: - student_a = os.path.join(batch_dir, "student_a") - student_b = os.path.join(batch_dir, "student_b") - os.makedirs(student_a) - os.makedirs(student_b) + student_a = str(Path(batch_dir, "student_a")) + student_b = str(Path(batch_dir, "student_b")) + Path(student_a).mkdir(parents=True) + Path(student_b).mkdir(parents=True) mock_get_args.return_value = { "student_id": "test_student", - "project_root": os.path.join(batch_dir, "*"), + "project_root": str(Path(batch_dir, "*")), "config": "/path/to/config", "report_format": "text", "verbosity": 1, @@ -362,15 +361,15 @@ def test_09_glob_project_root_grades_each_match( run_grader() # Assert - self.assertEqual( - mock_grader.return_value.grade.call_args_list, - [call(student_a, "student_a"), call(student_b, "student_b")], - ) + assert mock_grader.return_value.grade.call_args_list == [ + call(student_a, "student_a"), + call(student_b, "student_b"), + ] @patch("desktop.main.get_args") @patch("desktop.main.Grader") @patch("desktop.main.setup_logger") - @patch("desktop.main.extract_student_id_from_path", side_effect=os.path.basename) + @patch("desktop.main.extract_student_id_from_path", side_effect=lambda p: Path(p).name) def test_10_grader_error_for_one_project_does_not_stop_the_batch( self, _mock_extract_student_id: MagicMock, @@ -381,14 +380,14 @@ def test_10_grader_error_for_one_project_does_not_stop_the_batch( """Test that a GraderError for one project is logged and grading continues with the next one.""" # Arrange with tempfile.TemporaryDirectory() as batch_dir: - student_a = os.path.join(batch_dir, "student_a") - student_b = os.path.join(batch_dir, "student_b") - os.makedirs(student_a) - os.makedirs(student_b) + student_a = str(Path(batch_dir, "student_a")) + student_b = str(Path(batch_dir, "student_b")) + Path(student_a).mkdir(parents=True) + Path(student_b).mkdir(parents=True) mock_get_args.return_value = { "student_id": "test_student", - "project_root": os.path.join(batch_dir, "*"), + "project_root": str(Path(batch_dir, "*")), "config": "/path/to/config", "report_format": "text", "verbosity": 1, @@ -406,14 +405,14 @@ def test_10_grader_error_for_one_project_does_not_stop_the_batch( return_code = run_grader() # Assert - self.assertEqual(return_code, 1) - self.assertEqual(mock_grader.return_value.grade.call_count, 2) + assert return_code == 1 + assert mock_grader.return_value.grade.call_count == 2 mock_setup_logger.return_value.error.assert_called_once() @patch("desktop.main.get_args") @patch("desktop.main.Grader") @patch("desktop.main.setup_logger") - @patch("desktop.main.extract_student_id_from_path", side_effect=os.path.basename) + @patch("desktop.main.extract_student_id_from_path", side_effect=lambda p: Path(p).name) @patch("desktop.main.resolve_project_root") def test_10b_bad_zip_for_one_project_does_not_stop_the_batch( self, @@ -426,14 +425,14 @@ def test_10b_bad_zip_for_one_project_does_not_stop_the_batch( """Test that a corrupted zip for one project is logged and grading continues with the next one.""" # Arrange with tempfile.TemporaryDirectory() as batch_dir: - student_a = os.path.join(batch_dir, "student_a") - student_b = os.path.join(batch_dir, "student_b") - os.makedirs(student_a) - os.makedirs(student_b) + student_a = str(Path(batch_dir, "student_a")) + student_b = str(Path(batch_dir, "student_b")) + Path(student_a).mkdir(parents=True) + Path(student_b).mkdir(parents=True) mock_get_args.return_value = { "student_id": "test_student", - "project_root": os.path.join(batch_dir, "*"), + "project_root": str(Path(batch_dir, "*")), "config": "/path/to/config", "report_format": "text", "verbosity": 1, @@ -449,15 +448,15 @@ def test_10b_bad_zip_for_one_project_does_not_stop_the_batch( return_code = run_grader() # Assert - self.assertEqual(return_code, 1) - self.assertEqual(mock_grader.return_value.grade.call_count, 1) + assert return_code == 1 + assert mock_grader.return_value.grade.call_count == 1 mock_setup_logger.return_value.error.assert_called_once() @patch("desktop.main.get_args") @patch("desktop.main.Grader") @patch("desktop.main.setup_logger") @patch("desktop.main.shutil.rmtree") - @patch("desktop.main.os.path.exists") + @patch("pathlib.Path.exists") def test_11_work_dir_is_removed_when_present( self, mock_exists: MagicMock, @@ -489,57 +488,57 @@ def test_11_work_dir_is_removed_when_present( mock_rmtree.assert_called_once_with(const.WORK_DIR) -class TestExpandProjectRoot(unittest.TestCase): +class TestExpandProjectRoot: """Tests for the expand_project_root function.""" def test_01_literal_path_returned_unchanged(self) -> None: """Verify a plain path with no glob metacharacters is never passed to glob.glob.""" - self.assertEqual(expand_project_root("some/literal/path"), ["some/literal/path"]) + assert expand_project_root("some/literal/path") == ["some/literal/path"] def test_02_glob_returns_sorted_matches(self) -> None: """Verify a wildcard pattern expands to every match, sorted.""" with tempfile.TemporaryDirectory() as tmp_dir: - student_a = os.path.join(tmp_dir, "student_a") - student_b = os.path.join(tmp_dir, "student_b") - os.makedirs(student_b) - os.makedirs(student_a) + student_a = str(Path(tmp_dir, "student_a")) + student_b = str(Path(tmp_dir, "student_b")) + Path(student_b).mkdir(parents=True) + Path(student_a).mkdir(parents=True) - result = expand_project_root(os.path.join(tmp_dir, "*")) + result = expand_project_root(str(Path(tmp_dir, "*"))) - self.assertEqual(result, [student_a, student_b]) + assert result == [student_a, student_b] def test_03_glob_with_no_matches_falls_back_to_pattern(self) -> None: """Verify a wildcard pattern that matches nothing falls back to the raw pattern.""" pattern = "/nonexistent/pygrader_test_dir/*" - self.assertEqual(expand_project_root(pattern), [pattern]) + assert expand_project_root(pattern) == [pattern] def test_04_literal_path_with_brackets_not_treated_as_character_class(self) -> None: """Verify a literal path containing '[' is returned as-is instead of matched as a glob.""" pattern = "projects/[final]" - self.assertEqual(expand_project_root(pattern), [pattern]) + assert expand_project_root(pattern) == [pattern] -class TestResolveProjectRoot(unittest.TestCase): +class TestResolveProjectRoot: """Tests for the resolve_project_root function.""" def test_01_non_zip_path_returned_unchanged(self) -> None: """Verify a plain directory path is returned unchanged, without touching the filesystem.""" - self.assertEqual(resolve_project_root("some/project/dir"), "some/project/dir") + assert resolve_project_root("some/project/dir") == "some/project/dir" @patch("desktop.main.unzip_archive") @patch("desktop.main.is_path_zip", return_value=True) def test_02_zip_with_single_subfolder_is_flattened(self, _mock_is_zip: MagicMock, mock_unzip: MagicMock) -> None: """Verify that a single top-level subfolder in the extracted archive becomes the project root.""" with tempfile.TemporaryDirectory() as extracted_dir: - project_dir = os.path.join(extracted_dir, "project") - os.makedirs(project_dir) + project_dir = str(Path(extracted_dir, "project")) + Path(project_dir).mkdir(parents=True) mock_unzip.return_value = extracted_dir result = resolve_project_root("archive.zip") - self.assertEqual(result, project_dir) + assert result == project_dir @patch("desktop.main.unzip_archive") @patch("desktop.main.is_path_zip", return_value=True) @@ -548,10 +547,10 @@ def test_03_zip_with_multiple_subfolders_is_not_flattened( ) -> None: """Verify that multiple top-level subfolders leave the extraction root untouched.""" with tempfile.TemporaryDirectory() as extracted_dir: - os.makedirs(os.path.join(extracted_dir, "src")) - os.makedirs(os.path.join(extracted_dir, "tests")) + Path(str(Path(extracted_dir, "src"))).mkdir(parents=True) + Path(str(Path(extracted_dir, "tests"))).mkdir(parents=True) mock_unzip.return_value = extracted_dir result = resolve_project_root("archive.zip") - self.assertEqual(result, extracted_dir) + assert result == extracted_dir diff --git a/tests/unit/test_process.py b/tests/unit/test_process.py index ef4a267..266ec25 100644 --- a/tests/unit/test_process.py +++ b/tests/unit/test_process.py @@ -1,17 +1,17 @@ """Unit tests for the process module.""" import subprocess -import unittest +from collections.abc import Callable from unittest.mock import MagicMock, patch from grader.utils.process import run -class TestRunProcess(unittest.TestCase): +class TestRunProcess: """Test cases for the run function in the process module.""" @patch("subprocess.run") - def test_01_non_zero_return_code(self, mocked_subprocess: MagicMock) -> None: + def test_01_non_zero_return_code(self, mocked_subprocess: MagicMock, log_output: Callable[[], list[str]]) -> None: """ Test that the run function logs the correct messages and returns the expected result. @@ -33,24 +33,23 @@ def test_01_non_zero_return_code(self, mocked_subprocess: MagicMock) -> None: mocked_subprocess.return_value = expected_subprocess_result # Act - with self.assertLogs("grader", level="DEBUG") as log: - actual_subprocess_result = run([expected_command]) + actual_subprocess_result = run([expected_command]) - is_command_name_logged = expected_line in log.output - is_additional_information_logged = ( - f"DEBUG:grader:Command failed: {expected_returncode} {expected_stdout} {expected_stderr}" in log.output - ) + is_command_name_logged = expected_line in log_output() + is_additional_information_logged = ( + f"DEBUG:grader:Command failed: {expected_returncode} {expected_stdout} {expected_stderr}" in log_output() + ) # Assert - self.assertEqual(expected_subprocess_result, actual_subprocess_result) - self.assertTrue(is_command_name_logged) - self.assertTrue(is_additional_information_logged) + assert expected_subprocess_result == actual_subprocess_result + assert is_command_name_logged + assert is_additional_information_logged mocked_subprocess.assert_called_once_with( [expected_command], check=False, capture_output=True, text=True, cwd=None, env=None ) @patch("subprocess.run") - def test_02_zero_return_code(self, mocked_subprocess: MagicMock) -> None: + def test_02_zero_return_code(self, mocked_subprocess: MagicMock, log_output: Callable[[], list[str]]) -> None: """ Test that the run function logs the correct messages and returns the expected result. @@ -69,15 +68,14 @@ def test_02_zero_return_code(self, mocked_subprocess: MagicMock) -> None: mocked_subprocess.return_value = expected_subprocess_result # Act - with self.assertLogs("grader", level="DEBUG") as log: - actual_subprocess_result = run([expected_command]) - is_command_name_logged = expected_line in log.output - is_additional_information_logged = f"DEBUG:grader:Command succeeded: {expected_stdout}" in log.output + actual_subprocess_result = run([expected_command]) + is_command_name_logged = expected_line in log_output() + is_additional_information_logged = f"DEBUG:grader:Command succeeded: {expected_stdout}" in log_output() # Assert - self.assertEqual(expected_subprocess_result, actual_subprocess_result) - self.assertTrue(is_command_name_logged) - self.assertTrue(is_additional_information_logged) + assert expected_subprocess_result == actual_subprocess_result + assert is_command_name_logged + assert is_additional_information_logged mocked_subprocess.assert_called_once_with( [expected_command], check=False, capture_output=True, text=True, cwd=None, env=None ) diff --git a/tests/unit/test_pylint_check.py b/tests/unit/test_pylint_check.py index 10ab002..b0d435e 100644 --- a/tests/unit/test_pylint_check.py +++ b/tests/unit/test_pylint_check.py @@ -1,22 +1,22 @@ """Unit tests for the PylintCheck class.""" -import unittest from subprocess import CompletedProcess from unittest.mock import MagicMock, patch +import pytest + import grader.utils.constants as const from grader.checks.pylint_check import PylintCheck from grader.exceptions import CheckError -class TestPylintCheck(unittest.TestCase): +class TestPylintCheck: """Test cases for the PylintCheck class.""" - def setUp(self) -> None: + def setup_method(self) -> None: """Set up the test environment.""" self.pylint_check = PylintCheck("pylint", "sample_dir", 2, is_venv_required=False) # This way, we have 3 ranges: 0-33, 34-66, 67-100 - return super().setUp() @patch("grader.utils.process.run") @patch("grader.utils.files.find_all_python_files") @@ -41,22 +41,22 @@ def test_01_pylint_called(self, mocked_find_python_files: MagicMock, mocked_pyli mocked_pylint.assert_called_once() mocked_find_python_files.assert_called_once() # called_with[0] is *args. First arg is the list of "args" of lint.Run - self.assertIn("file1.py", called_with[0][0]) - self.assertIn("file2.py", called_with[0][0]) + assert "file1.py" in called_with[0][0] + assert "file2.py" in called_with[0][0] @patch("grader.utils.process.run") - @patch("os.path.exists") - def test_02_pylintrc_file_exists(self, mocked_os_path_exists: MagicMock, mocked_pylint: MagicMock) -> None: + @patch("pathlib.Path.exists") + def test_02_pylintrc_file_exists(self, mocked_path_exists: MagicMock, mocked_pylint: MagicMock) -> None: """ Test if pylint is called with the --rcfile argument when the pylintrc file exists. - :param mocked_os_path_exists: Mocked os.path.exists function. - :type mocked_os_path_exists: MagicMock + :param mocked_path_exists: Mocked os.path.exists function. + :type mocked_path_exists: MagicMock :param mocked_pylint: Mocked grader.utils.process.run function. :type mocked_pylint: MagicMock """ # Arrange - mocked_os_path_exists.return_value = True + mocked_path_exists.return_value = True mocked_pylint.return_value = CompletedProcess("pylint", 0, self.__create_sample_pylint_output(10)) # Act @@ -67,22 +67,22 @@ def test_02_pylintrc_file_exists(self, mocked_os_path_exists: MagicMock, mocked_ mocked_pylint.assert_called_once() # called_with[0] is *args. First arg is the list of "args" of lint.Run - self.assertIn("--rcfile", called_with[0][0]) - self.assertIn(const.PYLINTRC, called_with[0][0]) + assert "--rcfile" in called_with[0][0] + assert const.PYLINTRC in called_with[0][0] @patch("grader.utils.process.run") - @patch("os.path.exists") - def test_03_pylintrc_file_does_not_exist(self, mocked_os_path_exists: MagicMock, mocked_pylint: MagicMock) -> None: + @patch("pathlib.Path.exists") + def test_03_pylintrc_file_does_not_exist(self, mocked_path_exists: MagicMock, mocked_pylint: MagicMock) -> None: """ Test if pylint is called without the --rcfile argument when the pylintrc file does not exist. - :param mocked_os_path_exists: Mocked os.path.exists function. - :type mocked_os_path_exists: MagicMock + :param mocked_path_exists: Mocked os.path.exists function. + :type mocked_path_exists: MagicMock :param mocked_pylint: Mocked grader.utils.process.run function. :type mocked_pylint: MagicMock """ # Arrange - mocked_os_path_exists.return_value = False + mocked_path_exists.return_value = False mocked_pylint.return_value = CompletedProcess("pylint", 0, self.__create_sample_pylint_output(10)) # Act @@ -93,8 +93,8 @@ def test_03_pylintrc_file_does_not_exist(self, mocked_os_path_exists: MagicMock, mocked_pylint.assert_called_once() # called_with[0] is *args. First arg is the list of "args" of lint.Run - self.assertNotIn("--rcfile", called_with[0][0]) - self.assertNotIn(const.PYLINTRC, called_with[0][0]) + assert "--rcfile" not in called_with[0][0] + assert const.PYLINTRC not in called_with[0][0] @patch("grader.utils.process.run") def test_04_translate_score_zero(self, mocked_pylint: MagicMock) -> None: @@ -112,9 +112,9 @@ def test_04_translate_score_zero(self, mocked_pylint: MagicMock) -> None: actual_score = self.pylint_check.run() # Assert - self.assertEqual(1, actual_score.result) - self.assertEqual(self.pylint_check.name, actual_score.name) - self.assertEqual(self.pylint_check.max_points, actual_score.max_score) + assert 1 == actual_score.result + assert self.pylint_check.name == actual_score.name + assert self.pylint_check.max_points == actual_score.max_score @patch("grader.utils.process.run") def test_05_translate_score_inside_first_range(self, mocked_pylint: MagicMock) -> None: @@ -132,9 +132,9 @@ def test_05_translate_score_inside_first_range(self, mocked_pylint: MagicMock) - actual_score = self.pylint_check.run() # Assert - self.assertEqual(0, actual_score.result) - self.assertEqual(self.pylint_check.name, actual_score.name) - self.assertEqual(self.pylint_check.max_points, actual_score.max_score) + assert 0 == actual_score.result + assert self.pylint_check.name == actual_score.name + assert self.pylint_check.max_points == actual_score.max_score @patch("grader.utils.process.run") def test_06_translate_score_right_bound_first_range(self, mocked_pylint: MagicMock) -> None: @@ -152,9 +152,9 @@ def test_06_translate_score_right_bound_first_range(self, mocked_pylint: MagicMo actual_score = self.pylint_check.run() # Assert - self.assertEqual(1, actual_score.result) - self.assertEqual(self.pylint_check.name, actual_score.name) - self.assertEqual(self.pylint_check.max_points, actual_score.max_score) + assert 1 == actual_score.result + assert self.pylint_check.name == actual_score.name + assert self.pylint_check.max_points == actual_score.max_score @patch("grader.utils.process.run") def test_07_translate_score_left_bound_second_range(self, mocked_pylint: MagicMock) -> None: @@ -172,9 +172,9 @@ def test_07_translate_score_left_bound_second_range(self, mocked_pylint: MagicMo actual_score = self.pylint_check.run() # Assert - self.assertEqual(1, actual_score.result) - self.assertEqual(self.pylint_check.name, actual_score.name) - self.assertEqual(self.pylint_check.max_points, actual_score.max_score) + assert 1 == actual_score.result + assert self.pylint_check.name == actual_score.name + assert self.pylint_check.max_points == actual_score.max_score @patch("grader.utils.process.run") def test_08_translate_score_inside_bound_second_range(self, mocked_pylint: MagicMock) -> None: @@ -192,9 +192,9 @@ def test_08_translate_score_inside_bound_second_range(self, mocked_pylint: Magic actual_score = self.pylint_check.run() # Assert - self.assertEqual(1, actual_score.result) - self.assertEqual(self.pylint_check.name, actual_score.name) - self.assertEqual(self.pylint_check.max_points, actual_score.max_score) + assert 1 == actual_score.result + assert self.pylint_check.name == actual_score.name + assert self.pylint_check.max_points == actual_score.max_score @patch("grader.utils.process.run") def test_09_translate_score_right_bound_second_range(self, mocked_pylint: MagicMock) -> None: @@ -212,9 +212,9 @@ def test_09_translate_score_right_bound_second_range(self, mocked_pylint: MagicM actual_score = self.pylint_check.run() # Assert - self.assertEqual(2, actual_score.result) - self.assertEqual(self.pylint_check.name, actual_score.name) - self.assertEqual(self.pylint_check.max_points, actual_score.max_score) + assert 2 == actual_score.result + assert self.pylint_check.name == actual_score.name + assert self.pylint_check.max_points == actual_score.max_score @patch("grader.utils.process.run") def test_10_translate_score_inside_bound_third_range(self, mocked_pylint: MagicMock) -> None: @@ -232,9 +232,9 @@ def test_10_translate_score_inside_bound_third_range(self, mocked_pylint: MagicM actual_score = self.pylint_check.run() # Assert - self.assertEqual(2, actual_score.result) - self.assertEqual(self.pylint_check.name, actual_score.name) - self.assertEqual(self.pylint_check.max_points, actual_score.max_score) + assert 2 == actual_score.result + assert self.pylint_check.name == actual_score.name + assert self.pylint_check.max_points == actual_score.max_score @patch("grader.utils.process.run") def test_11_translate_score_max(self, mocked_pylint: MagicMock) -> None: @@ -252,9 +252,9 @@ def test_11_translate_score_max(self, mocked_pylint: MagicMock) -> None: actual_score = self.pylint_check.run() # Assert - self.assertEqual(2, actual_score.result) - self.assertEqual(self.pylint_check.name, actual_score.name) - self.assertEqual(self.pylint_check.max_points, actual_score.max_score) + assert 2 == actual_score.result + assert self.pylint_check.name == actual_score.name + assert self.pylint_check.max_points == actual_score.max_score @patch("grader.utils.files.find_all_python_files") def test_12_find_all_python_files_raises_os_error(self, mocked_find: MagicMock) -> None: @@ -263,7 +263,7 @@ def test_12_find_all_python_files_raises_os_error(self, mocked_find: MagicMock) mocked_find.side_effect = OSError("Test error") # Act - with self.assertRaises(CheckError): + with pytest.raises(CheckError): self.pylint_check.run() @patch("grader.utils.process.run") @@ -273,7 +273,7 @@ def test_13_run_pylint_raises_os_error(self, mocked_run: MagicMock) -> None: mocked_run.side_effect = OSError("Test error") # Act - with self.assertRaises(CheckError): + with pytest.raises(CheckError): self.pylint_check.run() @patch("grader.utils.process.run") @@ -283,7 +283,7 @@ def test_13_run_pylint_exits_with_non_zero(self, mocked_run: MagicMock) -> None: mocked_run.return_value = CompletedProcess("pylint", 1) # Act - with self.assertRaises(CheckError): + with pytest.raises(CheckError): self.pylint_check.run() @staticmethod @@ -308,13 +308,13 @@ def __create_sample_pylint_output(score: float) -> str: return "\n".join(content).format(score=score) @patch("grader.utils.process.run") - @patch("os.path.exists") - def test_pylint_with_custom_pylintrc(self, mocked_os_path_exists: MagicMock, mocked_pylint: MagicMock) -> None: + @patch("pathlib.Path.exists") + def test_pylint_with_custom_pylintrc(self, mocked_path_exists: MagicMock, mocked_pylint: MagicMock) -> None: """Test if pylint is called with the --rcfile argument when a custom pylintrc file is specified and exists.""" # Arrange custom_pylintrc = "custom_rc_file" pylint_check = PylintCheck("pylint", "sample_dir", 2, is_venv_required=False, pylintrc_path=custom_pylintrc) - mocked_os_path_exists.return_value = True + mocked_path_exists.return_value = True mocked_pylint.return_value = CompletedProcess("pylint", 0, self.__create_sample_pylint_output(10)) # Act @@ -323,19 +323,19 @@ def test_pylint_with_custom_pylintrc(self, mocked_os_path_exists: MagicMock, moc # Assert mocked_pylint.assert_called_once() - self.assertIn("--rcfile", called_with[0][0]) - self.assertIn(custom_pylintrc, called_with[0][0]) + assert "--rcfile" in called_with[0][0] + assert custom_pylintrc in called_with[0][0] @patch("grader.utils.process.run") - @patch("os.path.exists") + @patch("pathlib.Path.exists") def test_pylint_with_nonexistent_custom_pylintrc( - self, mocked_os_path_exists: MagicMock, mocked_pylint: MagicMock + self, mocked_path_exists: MagicMock, mocked_pylint: MagicMock ) -> None: """Test if pylint is called without the --rcfile argument when a custom pylintrc is specified but does not exist.""" # Arrange custom_pylintrc = "non_existent_rc_file" pylint_check = PylintCheck("pylint", "sample_dir", 2, is_venv_required=False, pylintrc_path=custom_pylintrc) - mocked_os_path_exists.return_value = False + mocked_path_exists.return_value = False mocked_pylint.return_value = CompletedProcess("pylint", 0, self.__create_sample_pylint_output(10)) # Act @@ -344,5 +344,5 @@ def test_pylint_with_nonexistent_custom_pylintrc( # Assert mocked_pylint.assert_called_once() - self.assertNotIn("--rcfile", called_with[0][0]) - self.assertNotIn(custom_pylintrc, called_with[0][0]) + assert "--rcfile" not in called_with[0][0] + assert custom_pylintrc not in called_with[0][0] diff --git a/tests/unit/test_requirements_check.py b/tests/unit/test_requirements_check.py index 055bc25..8362194 100644 --- a/tests/unit/test_requirements_check.py +++ b/tests/unit/test_requirements_check.py @@ -1,6 +1,5 @@ """Unit tests for the RequirementsCheck class in the requirements_check module.""" -import unittest from unittest.mock import MagicMock, patch from grader.checks.requirements_check import RequirementsCheck @@ -8,13 +7,12 @@ from grader.models.check_result import ScoredCheckResult -class TestRequirementsCheck(unittest.TestCase): +class TestRequirementsCheck: """Unit tests for the RequirementsCheck class.""" - def setUp(self) -> None: + def setup_method(self) -> None: """Set up the test case environment.""" self.requirements_check = RequirementsCheck("requirements", "sample_dir", 1, is_venv_required=False) - return super().setUp() @patch("pathlib.Path.exists") def test_01_requirements_exist(self, mocked_exists: MagicMock) -> None: @@ -27,7 +25,7 @@ def test_01_requirements_exist(self, mocked_exists: MagicMock) -> None: actual_score = self.requirements_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("pathlib.Path.exists") def test_02_requirements_does_not_exist(self, mocked_exists: MagicMock) -> None: @@ -46,7 +44,7 @@ def test_02_requirements_does_not_exist(self, mocked_exists: MagicMock) -> None: actual_score = self.requirements_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("grader.checks.requirements_check.VirtualEnvironment") @patch("pathlib.Path.exists") @@ -74,7 +72,7 @@ def test_03_is_checking_install_with_successful_venv_setup( actual_score = requirements_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score mocked_venv_class.assert_called_once_with("sample_dir", is_keeping_existing_venv=True) mocked_venv_instance.__enter__.assert_called_once() mocked_venv_instance.__exit__.assert_called_once() @@ -106,7 +104,7 @@ def test_04_is_checking_install_with_failed_venv_setup( actual_score = requirements_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score mocked_venv_class.assert_called_once_with("sample_dir", is_keeping_existing_venv=True) mocked_venv_instance.__enter__.assert_called_once() mocked_venv_instance.__exit__.assert_not_called() @@ -133,7 +131,7 @@ def test_05_is_checking_install_false_does_not_create_venv( actual_score = requirements_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score mocked_venv_class.assert_not_called() @patch("grader.checks.requirements_check.VirtualEnvironment") @@ -164,5 +162,5 @@ def test_06_is_checking_install_true_but_no_requirements_file( actual_score = requirements_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score mocked_venv_class.assert_not_called() diff --git a/tests/unit/test_results_reporter.py b/tests/unit/test_results_reporter.py index b79906e..c9ebfba 100644 --- a/tests/unit/test_results_reporter.py +++ b/tests/unit/test_results_reporter.py @@ -1,20 +1,20 @@ """Unit tests for the reporter classes in the results_reporter module.""" import json -import unittest + +import pytest from grader.models.check_result import CheckResult, NonScoredCheckResult, ScoredCheckResult from grader.models.grading_result import GradingResult from grader.utils.results_reporter import CSVResultsReporter, JSONResultsReporter, PlainTextResultsReporter -class TestJSONResultsReporter(unittest.TestCase): +class TestJSONResultsReporter: """Test cases for the JSONResultsReporter class.""" - def setUp(self) -> None: + def setup_method(self) -> None: """Set up the JSONResultsReporter instance for testing.""" self.reporter = JSONResultsReporter() - return super().setUp() def test_01_no_results_returns_empty_list(self) -> None: """Test that to_string returns an empty JSON array when no results were added.""" @@ -22,7 +22,7 @@ def test_01_no_results_returns_empty_list(self) -> None: actual = json.loads(self.reporter.to_string()) # Assert - self.assertEqual(actual, []) + assert actual == [] def test_02_single_run_non_verbose(self) -> None: """Test that a non-verbose run serializes scored and non-scored checks without info/error.""" @@ -47,7 +47,7 @@ def test_02_single_run_non_verbose(self) -> None: actual = json.loads(self.reporter.to_string()) # Assert - self.assertEqual(actual, expected) + assert actual == expected def test_03_verbose_includes_info_and_error(self) -> None: """Test that a verbose run includes the info and error fields for each check.""" @@ -77,7 +77,7 @@ def test_03_verbose_includes_info_and_error(self) -> None: actual = json.loads(self.reporter.to_string()) # Assert - self.assertEqual(actual, expected) + assert actual == expected def test_04_multiple_runs_are_all_included(self) -> None: """Test that adding multiple GradingResults includes one entry per run in the output.""" @@ -89,7 +89,7 @@ def test_04_multiple_runs_are_all_included(self) -> None: actual = json.loads(self.reporter.to_string()) # Assert - self.assertEqual([run["run_id"] for run in actual], ["student_1", "student_2"]) + assert [run["run_id"] for run in actual] == ["student_1", "student_2"] def test_05_unknown_check_result_type_raises_value_error(self) -> None: """Test that a CheckResult that is neither scored nor non-scored raises a ValueError.""" @@ -98,18 +98,17 @@ def test_05_unknown_check_result_type_raises_value_error(self) -> None: self.reporter.add_result(GradingResult("student_1", 0, 0, [unknown_result])) # Act & Assert - with self.assertRaises(ValueError) as context: + with pytest.raises(ValueError) as context: self.reporter.to_string() - self.assertIn("mystery", str(context.exception)) + assert "mystery" in str(context.value) -class TestCSVResultsReporter(unittest.TestCase): +class TestCSVResultsReporter: """Test cases for the CSVResultsReporter class.""" - def setUp(self) -> None: + def setup_method(self) -> None: """Set up the CSVResultsReporter instance for testing.""" self.reporter = CSVResultsReporter() - return super().setUp() def test_01_no_results_returns_only_header(self) -> None: """Test that to_string returns only the header row when no results were added.""" @@ -117,7 +116,7 @@ def test_01_no_results_returns_only_header(self) -> None: actual = self.reporter.to_string() # Assert - self.assertEqual(actual, "Run ID,Check,Score,Max Score\n") + assert actual == "Run ID,Check,Score,Max Score\n" def test_02_single_run_non_verbose(self) -> None: """Test that a non-verbose run emits one row per check without info/error columns.""" @@ -132,7 +131,7 @@ def test_02_single_run_non_verbose(self) -> None: actual = self.reporter.to_string() # Assert - self.assertEqual(actual, expected) + assert actual == expected def test_03_verbose_includes_info_and_error_columns(self) -> None: """Test that a verbose run includes the info and error columns for each check.""" @@ -152,7 +151,7 @@ def test_03_verbose_includes_info_and_error_columns(self) -> None: actual = self.reporter.to_string() # Assert - self.assertEqual(actual, expected) + assert actual == expected def test_04_multiple_runs_include_run_id_per_row(self) -> None: """Test that rows from different runs are each tagged with their own run id.""" @@ -166,7 +165,7 @@ def test_04_multiple_runs_include_run_id_per_row(self) -> None: actual = self.reporter.to_string() # Assert - self.assertEqual(actual, expected) + assert actual == expected def test_05_unknown_check_result_type_raises_value_error(self) -> None: """Test that a CheckResult that is neither scored nor non-scored raises a ValueError.""" @@ -175,18 +174,17 @@ def test_05_unknown_check_result_type_raises_value_error(self) -> None: self.reporter.add_result(GradingResult("student_1", 0, 0, [unknown_result])) # Act & Assert - with self.assertRaises(ValueError) as context: + with pytest.raises(ValueError) as context: self.reporter.to_string() - self.assertIn("mystery", str(context.exception)) + assert "mystery" in str(context.value) -class TestPlainTextResultsReporter(unittest.TestCase): +class TestPlainTextResultsReporter: """Test cases for the PlainTextResultsReporter class.""" - def setUp(self) -> None: + def setup_method(self) -> None: """Set up the PlainTextResultsReporter instance for testing.""" self.reporter = PlainTextResultsReporter() - return super().setUp() def test_01_no_results_returns_empty_string(self) -> None: """Test that to_string returns a single newline when no results were added.""" @@ -194,7 +192,7 @@ def test_01_no_results_returns_empty_string(self) -> None: actual = self.reporter.to_string() # Assert - self.assertEqual(actual, "\n") + assert actual == "\n" def test_02_single_run_non_verbose(self) -> None: """Test that a non-verbose run emits one line per check plus a total line, without info/error.""" @@ -213,7 +211,7 @@ def test_02_single_run_non_verbose(self) -> None: actual = self.reporter.to_string() # Assert - self.assertEqual(actual, expected) + assert actual == expected def test_03_verbose_includes_info_and_error_when_present(self) -> None: """Test that a verbose run appends Info/Error segments when they are non-empty.""" @@ -231,7 +229,7 @@ def test_03_verbose_includes_info_and_error_when_present(self) -> None: actual = self.reporter.to_string() # Assert - self.assertEqual(actual, expected) + assert actual == expected def test_04_verbose_omits_info_and_error_when_empty(self) -> None: """Test that a verbose run does not append Info/Error segments when they are empty strings.""" @@ -246,7 +244,7 @@ def test_04_verbose_omits_info_and_error_when_empty(self) -> None: actual = self.reporter.to_string() # Assert - self.assertEqual(actual, expected) + assert actual == expected def test_05_multiple_runs_each_get_their_own_total_line(self) -> None: """Test that each added run produces its own group of check lines and total line.""" @@ -265,7 +263,7 @@ def test_05_multiple_runs_each_get_their_own_total_line(self) -> None: actual = self.reporter.to_string() # Assert - self.assertEqual(actual, expected) + assert actual == expected def test_06_unknown_check_result_type_raises_value_error(self) -> None: """Test that a CheckResult that is neither scored nor non-scored raises a ValueError.""" @@ -274,18 +272,17 @@ def test_06_unknown_check_result_type_raises_value_error(self) -> None: self.reporter.add_result(GradingResult("student_1", 0, 0, [unknown_result])) # Act & Assert - with self.assertRaises(ValueError) as context: + with pytest.raises(ValueError) as context: self.reporter.to_string() - self.assertIn("mystery", str(context.exception)) + assert "mystery" in str(context.value) -class TestResultsReporterAddResult(unittest.TestCase): +class TestResultsReporterAddResult: """Test cases for the shared ResultsReporter.add_result behavior.""" - def setUp(self) -> None: + def setup_method(self) -> None: """Set up a concrete ResultsReporter instance for testing, since the base class is abstract.""" self.reporter = PlainTextResultsReporter() - return super().setUp() def test_01_add_result_appends_to_results(self) -> None: """Test that add_result appends the given GradingResult to the internal results list.""" @@ -298,4 +295,4 @@ def test_01_add_result_appends_to_results(self) -> None: self.reporter.add_result(second_result) # Assert - self.assertEqual(self.reporter._results, [first_result, second_result]) # pylint: disable=protected-access + assert self.reporter._results == [first_result, second_result] diff --git a/tests/unit/test_spec_check.py b/tests/unit/test_spec_check.py index b24ea6d..baba9ce 100644 --- a/tests/unit/test_spec_check.py +++ b/tests/unit/test_spec_check.py @@ -1,9 +1,9 @@ """Unit tests for the SpecCheck class in the spec_check module.""" import json -import unittest from unittest.mock import MagicMock, patch +import pytest import requests from grader.checks.spec_check import SpecCheck @@ -20,14 +20,18 @@ def _make_response(status_code: int = 200, json_body: dict | None = None, text: return response -class TestSpecCheck(unittest.TestCase): +class TestSpecCheck: """Unit tests for the SpecCheck class.""" - def setUp(self) -> None: + def setup_method(self) -> None: """Set up the test environment.""" self.spec_check = SpecCheck("spec", "sample_dir", 10, is_venv_required=False) self.spec_check._assets = [MagicMock(read=MagicMock(return_value="# Spec\nDo the thing."))] - return super().setUp() + + def teardown_method(self) -> None: + """Stop the open() patcher if a test started one.""" + if hasattr(self, "open_patcher"): + self.open_patcher.stop() def _mock_source(self, mocked_find_all_source_files: MagicMock, files: dict) -> None: """Point find_all_source_files at fake files, patching open() to serve their contents.""" @@ -39,7 +43,7 @@ def fake_open(path: str, *_args: object, **_kwargs: object) -> MagicMock: return handle patcher = patch("builtins.open", side_effect=fake_open) - self.addCleanup(patcher.stop) + self.open_patcher = patcher patcher.start() @patch("grader.checks.spec_check.requests.post") @@ -58,7 +62,7 @@ def test_01_happy_path(self, mocked_find_all_source_files: MagicMock, mocked_pos actual = self.spec_check.run() # Assert - self.assertEqual(expected, actual) + assert expected == actual @patch("grader.checks.spec_check.requests.post") @patch("grader.checks.spec_check.find_all_source_files") @@ -77,7 +81,7 @@ def test_02_score_above_max_is_clamped( actual = self.spec_check.run() # Assert - self.assertEqual(actual.result, 10.0) + assert actual.result == 10.0 @patch("grader.checks.spec_check.requests.post") @patch("grader.checks.spec_check.find_all_source_files") @@ -96,11 +100,11 @@ def test_03_negative_score_is_clamped_to_zero( actual = self.spec_check.run() # Assert - self.assertEqual(actual.result, 0.0) + assert actual.result == 0.0 def test_04_missing_api_key_raises_check_error(self) -> None: """Verify that a missing API key environment variable raises a CheckError.""" - with patch.dict("os.environ", {}, clear=True), self.assertRaises(CheckError): + with patch.dict("os.environ", {}, clear=True), pytest.raises(CheckError): self.spec_check.run() def test_05_missing_assets_raises_check_error(self) -> None: @@ -109,7 +113,7 @@ def test_05_missing_assets_raises_check_error(self) -> None: self.spec_check._assets = [] # Act & Assert - with self.assertRaises(CheckError): + with pytest.raises(CheckError): self.spec_check.run() def test_06_unreadable_spec_file_raises_check_error(self) -> None: @@ -118,7 +122,7 @@ def test_06_unreadable_spec_file_raises_check_error(self) -> None: self.spec_check._assets = [MagicMock(read=MagicMock(side_effect=ResourceError("boom")))] # Act & Assert - with self.assertRaises(CheckError): + with pytest.raises(CheckError): self.spec_check.run() @patch("grader.checks.spec_check.requests.post") @@ -132,10 +136,10 @@ def test_07_non_2xx_response_raises_check_error( mocked_post.return_value = _make_response(500, text="internal error") # Act & Assert - with patch.dict("os.environ", {"OPENROUTER_API_KEY": "test_key"}), self.assertRaises(CheckError) as context: + with patch.dict("os.environ", {"OPENROUTER_API_KEY": "test_key"}), pytest.raises(CheckError) as context: self.spec_check.run() - self.assertIn("500", str(context.exception)) + assert "500" in str(context.value) @patch("grader.checks.spec_check.requests.post") @patch("grader.checks.spec_check.find_all_source_files") @@ -148,7 +152,7 @@ def test_08_network_error_raises_check_error( mocked_post.side_effect = requests.ConnectionError("network down") # Act & Assert - with patch.dict("os.environ", {"OPENROUTER_API_KEY": "test_key"}), self.assertRaises(CheckError): + with patch.dict("os.environ", {"OPENROUTER_API_KEY": "test_key"}), pytest.raises(CheckError): self.spec_check.run() @patch("grader.checks.spec_check.requests.post") @@ -162,7 +166,7 @@ def test_09_error_in_200_response_raises_check_error( mocked_post.return_value = _make_response(200, {"error": {"message": "no credits"}}) # Act & Assert - with patch.dict("os.environ", {"OPENROUTER_API_KEY": "test_key"}), self.assertRaises(CheckError): + with patch.dict("os.environ", {"OPENROUTER_API_KEY": "test_key"}), pytest.raises(CheckError): self.spec_check.run() @patch("grader.checks.spec_check.requests.post") @@ -176,7 +180,7 @@ def test_10_non_json_model_output_raises_check_error( mocked_post.return_value = _make_response(200, {"choices": [{"message": {"content": "not json at all"}}]}) # Act & Assert - with patch.dict("os.environ", {"OPENROUTER_API_KEY": "test_key"}), self.assertRaises(CheckError): + with patch.dict("os.environ", {"OPENROUTER_API_KEY": "test_key"}), pytest.raises(CheckError): self.spec_check.run() @patch("grader.checks.spec_check.requests.post") @@ -196,7 +200,7 @@ def test_11_fenced_json_output_parses_correctly( actual = self.spec_check.run() # Assert - self.assertEqual(expected, actual) + assert expected == actual @patch("grader.checks.spec_check.requests.post") @patch("grader.checks.spec_check.find_all_source_files") @@ -217,6 +221,6 @@ def test_12_source_sent_to_model_excludes_test_files( # Assert sent_payload = mocked_post.call_args.kwargs["json"] user_message = sent_payload["messages"][1]["content"] - self.assertIn("main.py", user_message) - self.assertIn("print('hi')", user_message) - self.assertNotIn("test_main.py", user_message) + assert "main.py" in user_message + assert "print('hi')" in user_message + assert "test_main.py" not in user_message diff --git a/tests/unit/test_structure_check.py b/tests/unit/test_structure_check.py index f4f1100..d6672f0 100644 --- a/tests/unit/test_structure_check.py +++ b/tests/unit/test_structure_check.py @@ -1,21 +1,22 @@ """Unit tests for the StructureCheck class in the structure_check module.""" import json -import unittest +from collections.abc import Callable from unittest.mock import MagicMock, patch +import pytest + from grader.checks.structure_check import StructureCheck from grader.exceptions import CheckError, ResourceError from grader.models.check_result import NonScoredCheckResult -class TestStructureCheck(unittest.TestCase): +class TestStructureCheck: """Test cases for the StructureCheck class.""" - def setUp(self) -> None: + def setup_method(self) -> None: """Set up the test environment.""" self.structure_check = StructureCheck("structure", "sample_dir", "structure.json", is_venv_required=False) - return super().setUp() @patch("grader.checks.structure_check.StructureCheck._StructureCheck__load_structure_file") def test_01_valid_structure(self, mock_load_structure_file: MagicMock) -> None: @@ -31,7 +32,7 @@ def test_01_valid_structure(self, mock_load_structure_file: MagicMock) -> None: result = self.structure_check.run() # Assert - self.assertEqual(result, expected) + assert result == expected @patch("grader.checks.structure_check.StructureCheck._StructureCheck__load_structure_file") def test_02_invalid_required_structure(self, mock_load_structure_file: MagicMock) -> None: @@ -51,7 +52,7 @@ def test_02_invalid_required_structure(self, mock_load_structure_file: MagicMock result = self.structure_check.run() # Assert - self.assertEqual(result, expected) + assert result == expected @patch("grader.checks.structure_check.StructureCheck._StructureCheck__load_structure_file") def test_03_invalid_non_required_structure(self, mock_load_structure_file: MagicMock) -> None: @@ -68,7 +69,7 @@ def test_03_invalid_non_required_structure(self, mock_load_structure_file: Magic result = self.structure_check.run() # Assert - self.assertEqual(result, expected) + assert result == expected @patch("grader.checks.structure_check.StructureCheck._StructureCheck__load_structure_file") def test_04_empty_structure_file(self, mock_load_structure_file: MagicMock) -> None: @@ -81,10 +82,14 @@ def test_04_empty_structure_file(self, mock_load_structure_file: MagicMock) -> N result = self.structure_check.run() # Assert - self.assertEqual(result, expected) + assert result == expected @patch("grader.checks.structure_check.StructureCheck._StructureCheck__load_structure_file") - def test_05_logs_structure_validity(self, mock_load_structure_file: MagicMock) -> None: + def test_05_logs_structure_validity( + self, + mock_load_structure_file: MagicMock, + log_output: Callable[[], list[str]], + ) -> None: """Verify that the run method logs the validity of each structure element.""" # Arrange mock_element = MagicMock() @@ -94,12 +99,10 @@ def test_05_logs_structure_validity(self, mock_load_structure_file: MagicMock) - mock_load_structure_file.return_value = [mock_element] expected_log_output = "Is test_element structure valid ?" - with self.assertLogs("grader", level="DEBUG") as log: - # Act - self.structure_check.run() + self.structure_check.run() # Assert - self.assertTrue(any(expected_log_output in message for message in log.output)) + assert any(expected_log_output in message for message in log_output()) @patch("grader.checks.structure_check.StructureCheck._StructureCheck__load_structure_file") def test_06_raises_check_error_on_invalid_structure_file(self, mock_load_structure_file: MagicMock) -> None: @@ -108,7 +111,7 @@ def test_06_raises_check_error_on_invalid_structure_file(self, mock_load_structu mock_load_structure_file.side_effect = CheckError("Invalid structure file") # Act & Assert - with self.assertRaises(CheckError): + with pytest.raises(CheckError): self.structure_check.run() @patch("grader.checks.structure_check.StructureCheck._StructureCheck__load_structure_file") @@ -130,7 +133,7 @@ def test_07_multiple_valid_elements(self, mock_load_structure_file: MagicMock) - result = self.structure_check.run() # Assert - self.assertEqual(result, expected) + assert result == expected @patch("grader.checks.structure_check.StructureCheck._StructureCheck__load_structure_file") def test_08_multiple_elements_with_invalid_required(self, mock_load_structure_file: MagicMock) -> None: @@ -154,7 +157,7 @@ def test_08_multiple_elements_with_invalid_required(self, mock_load_structure_fi result = self.structure_check.run() # Assert - self.assertEqual(result, expected) + assert result == expected @patch("grader.checks.structure_check.StructureCheck._StructureCheck__load_structure_file") def test_09_multiple_elements_with_invalid_non_required(self, mock_load_structure_file: MagicMock) -> None: @@ -175,7 +178,7 @@ def test_09_multiple_elements_with_invalid_non_required(self, mock_load_structur result = self.structure_check.run() # Assert - self.assertEqual(result, expected) + assert result == expected @patch("grader.checks.structure_check.StructureCheck._StructureCheck__load_structure_file") def test_10_all_invalid_elements(self, mock_load_structure_file: MagicMock) -> None: @@ -202,7 +205,7 @@ def test_10_all_invalid_elements(self, mock_load_structure_file: MagicMock) -> N result = self.structure_check.run() # Assert - self.assertEqual(result, expected) + assert result == expected @patch("grader.utils.structure_validator.StructureValidator.is_structure_valid") @patch("grader.checks.structure_check.Resource") @@ -225,7 +228,7 @@ def test_11_load_structure_file_valid(self, mock_resource_cls: MagicMock, mock_s result = self.structure_check.run() # Assert - self.assertEqual(result, expected) + assert result == expected mock_resource_cls.assert_called_once_with("structure.json") @patch("grader.checks.structure_check.Resource") @@ -235,9 +238,9 @@ def test_12_load_structure_file_invalid(self, mock_resource_cls: MagicMock) -> N mock_resource_cls.return_value.read.return_value = json.dumps({"first": {"second": -1}}) # Act & Assert - with self.assertRaises(CheckError) as context: + with pytest.raises(CheckError) as context: self.structure_check.run() - self.assertIn("Invalid structure file", str(context.exception)) + assert "Invalid structure file" in str(context.value) @patch("grader.checks.structure_check.Resource") def test_13_load_structure_file_empty(self, mock_resource_cls: MagicMock) -> None: @@ -250,7 +253,7 @@ def test_13_load_structure_file_empty(self, mock_resource_cls: MagicMock) -> Non result = self.structure_check.run() # Assert - self.assertEqual(result, expected) + assert result == expected @patch("grader.checks.structure_check.Resource") def test_14_load_structure_file_invalid_json(self, mock_resource_cls: MagicMock) -> None: @@ -259,9 +262,9 @@ def test_14_load_structure_file_invalid_json(self, mock_resource_cls: MagicMock) mock_resource_cls.return_value.read.return_value = "not valid json" # Act & Assert - with self.assertRaises(CheckError) as context: + with pytest.raises(CheckError) as context: self.structure_check.run() - self.assertIn("Invalid structure file", str(context.exception)) + assert "Invalid structure file" in str(context.value) @patch("grader.checks.structure_check.Resource") def test_15_load_structure_file_not_found(self, mock_resource_cls: MagicMock) -> None: @@ -270,9 +273,9 @@ def test_15_load_structure_file_not_found(self, mock_resource_cls: MagicMock) -> mock_resource_cls.return_value.read.side_effect = ResourceError("Local file not found: structure.json") # Act & Assert - with self.assertRaises(CheckError) as context: + with pytest.raises(CheckError) as context: self.structure_check.run() - self.assertIn("Cannot read structure file", str(context.exception)) + assert "Cannot read structure file" in str(context.value) @patch("grader.checks.structure_check.Resource") def test_16_load_structure_file_remote_download_fails(self, mock_resource_cls: MagicMock) -> None: @@ -284,7 +287,7 @@ def test_16_load_structure_file_remote_download_fails(self, mock_resource_cls: M ) # Act & Assert - with self.assertRaises(CheckError) as context: + with pytest.raises(CheckError) as context: structure_check.run() - self.assertIn("Cannot read structure file", str(context.exception)) + assert "Cannot read structure file" in str(context.value) mock_resource_cls.assert_called_once_with("https://example.com/structure.json") diff --git a/tests/unit/test_structure_validator.py b/tests/unit/test_structure_validator.py index 35013dc..02d839a 100644 --- a/tests/unit/test_structure_validator.py +++ b/tests/unit/test_structure_validator.py @@ -1,21 +1,21 @@ """Unit tests for the StructureValidator class in the structure_validator module.""" import os -import unittest from pathlib import Path from unittest.mock import MagicMock, patch +import pytest + from grader.utils.structure_validator import StructureValidator -class TestStructureValidator(unittest.TestCase): +class TestStructureValidator: """Test cases for the StructureValidator class.""" - def setUp(self) -> None: + def setup_method(self) -> None: """Set up the test case environment.""" self.validator = StructureValidator(name="TestStructure", required=True, patterns=["*.py", "tests/*.py"]) self.project_root = "sample_project" - return super().setUp() @patch("pathlib.Path.glob") def test_01_is_structure_valid_all_patterns_match(self, mocked_glob: MagicMock) -> None: @@ -27,7 +27,7 @@ def test_01_is_structure_valid_all_patterns_match(self, mocked_glob: MagicMock) result = self.validator.is_structure_valid(self.project_root) # Assert - self.assertTrue(result) + assert result mocked_glob.assert_any_call("*.py") mocked_glob.assert_any_call("tests/*.py") @@ -41,20 +41,20 @@ def test_02_is_structure_valid_some_patterns_do_not_match(self, mocked_glob: Mag result = self.validator.is_structure_valid(self.project_root) # Assert - self.assertFalse(result) + assert not result mocked_glob.assert_any_call("*.py") mocked_glob.assert_any_call("tests/*.py") - @unittest.skipIf(os.name == "nt", "Skipping test on Windows due to path normalization.") + @pytest.mark.skipif(os.name == "nt", reason="Skipping test on Windows due to path normalization.") @patch("pathlib.Path.glob") def test_03_get_matching_files(self, mocked_glob: MagicMock) -> None: """Test that get_matching_files returns the correct list of matching files.""" # Arrange - project_root_abs = os.path.abspath(self.project_root).lower() # Normalize path for Windows + project_root_abs = str(Path(self.project_root).absolute()).lower() # Normalize path for Windows matching_files = [ - os.path.join(project_root_abs, "file1.py"), - os.path.join(project_root_abs, "file2.py"), - os.path.join(project_root_abs, "tests/test_file.py"), + str(Path(project_root_abs, "file1.py")), + str(Path(project_root_abs, "file2.py")), + str(Path(project_root_abs, "tests/test_file.py")), ] mocked_glob.side_effect = [ [Path(file) for file in matching_files[:2]], @@ -69,7 +69,7 @@ def test_03_get_matching_files(self, mocked_glob: MagicMock) -> None: actual_result = [filepath.lower() for filepath in actual_result] # Assert - self.assertEqual(actual_result, expected_result) + assert actual_result == expected_result mocked_glob.assert_any_call("*.py") mocked_glob.assert_any_call("tests/*.py") @@ -82,9 +82,9 @@ def test_04_from_dict(self) -> None: validator = StructureValidator.from_dict(raw_object) # Assert - self.assertEqual(validator.name, "SampleStructure") - self.assertFalse(validator.required) - self.assertEqual(validator.patterns, ["*.md", "docs/*.md"]) + assert validator.name == "SampleStructure" + assert not validator.required + assert validator.patterns == ["*.md", "docs/*.md"] def test_05_init(self) -> None: """Test that the __init__ method correctly initializes attributes.""" @@ -97,9 +97,9 @@ def test_05_init(self) -> None: validator = StructureValidator(name, required, patterns) # Assert - self.assertEqual(validator.name, name) - self.assertTrue(validator.required) - self.assertEqual(validator.patterns, patterns) + assert validator.name == name + assert validator.required + assert validator.patterns == patterns @patch("pathlib.Path.glob") def test_06_is_structure_valid_empty_patterns(self, mocked_glob: MagicMock) -> None: @@ -111,7 +111,7 @@ def test_06_is_structure_valid_empty_patterns(self, mocked_glob: MagicMock) -> N result = self.validator.is_structure_valid(self.project_root) # Assert - self.assertTrue(result) + assert result mocked_glob.assert_not_called() @patch("pathlib.Path.glob") @@ -124,7 +124,7 @@ def test_07_get_matching_files_empty_patterns(self, mocked_glob: MagicMock) -> N result = self.validator.get_matching_files(self.project_root) # Assert - self.assertEqual(result, []) + assert result == [] mocked_glob.assert_not_called() @patch("pathlib.Path.glob") @@ -134,7 +134,7 @@ def test_08_is_structure_valid_invalid_project_root(self, mocked_glob: MagicMock mocked_glob.side_effect = FileNotFoundError # Act - with self.assertRaises(FileNotFoundError): + with pytest.raises(FileNotFoundError): self.validator.is_structure_valid("invalid_path") @patch("pathlib.Path.glob") @@ -144,5 +144,5 @@ def test_09_get_matching_files_invalid_project_root(self, mocked_glob: MagicMock mocked_glob.side_effect = FileNotFoundError # Act - with self.assertRaises(FileNotFoundError): + with pytest.raises(FileNotFoundError): self.validator.get_matching_files("invalid_path") diff --git a/tests/unit/test_tests_check.py b/tests/unit/test_tests_check.py index 97d5718..7256155 100644 --- a/tests/unit/test_tests_check.py +++ b/tests/unit/test_tests_check.py @@ -1,18 +1,19 @@ """Unit tests for the TestsCheck class.""" -import unittest +from collections.abc import Callable from unittest.mock import MagicMock, patch +import pytest + from grader.checks.run_tests_check import RunTestsCheck from grader.exceptions import CheckError from grader.models.check_result import ScoredCheckResult -from grader.utils.logger import VERBOSE -class TestDownloadTest(unittest.TestCase): +class TestDownloadTest: """Unit tests for resolving tests_path via Resource in _pre_run.""" - def setUp(self) -> None: + def setup_method(self) -> None: """Set up a RunTestsCheck instance for testing.""" self.name = "Test Check" self.project_root = "/path/to/project" @@ -32,7 +33,7 @@ def test_01_path_resolved_via_resource_to_file(self, mock_resource_cls: MagicMoc # Assert mock_resource_cls.assert_called_once_with(path) - self.assertEqual(check._RunTestsCheck__tests_path, ["/tmp/test_file.py"]) # type: ignore[attr-defined] + assert check._RunTestsCheck__tests_path == ["/tmp/test_file.py"] # type: ignore[attr-defined] @patch("grader.checks.run_tests_check.Resource") def test_02_mixed_paths_processed_correctly(self, mock_resource_cls: MagicMock) -> None: @@ -56,16 +57,13 @@ def test_02_mixed_paths_processed_correctly(self, mock_resource_cls: MagicMock) check._pre_run() # Assert - self.assertEqual( - check._RunTestsCheck__tests_path, # type: ignore[attr-defined] - ["/tmp/test_cove.py", "/tmp/test_remote.py", local_path], - ) + assert check._RunTestsCheck__tests_path == ["/tmp/test_cove.py", "/tmp/test_remote.py", local_path] # type: ignore[attr-defined] -class TestTestsCheck(unittest.TestCase): +class TestTestsCheck: """Test cases for the TestsCheck class.""" - def setUp(self) -> None: + def setup_method(self) -> None: """Set up the test environment.""" self.name = "Test Check" self.project_root = "/path/to/project" @@ -97,7 +95,7 @@ def test_01_all_tests_pass(self, mock_pytest_run: MagicMock) -> None: score = self.tests_check.run() # Assert - self.assertEqual(score, expected_score) + assert score == expected_score @patch("grader.checks.run_tests_check.RunTestsCheck._RunTestsCheck__pytest_run") def test_02_some_tests_fail(self, mock_pytest_run: MagicMock) -> None: @@ -111,7 +109,7 @@ def test_02_some_tests_fail(self, mock_pytest_run: MagicMock) -> None: score = self.tests_check.run() # Assert - self.assertEqual(score, expected_score) + assert score == expected_score @patch("grader.checks.run_tests_check.RunTestsCheck._RunTestsCheck__pytest_run") def test_03_score_exceeds_max_points(self, mock_pytest_run: MagicMock) -> None: @@ -132,22 +130,25 @@ def test_03_score_exceeds_max_points(self, mock_pytest_run: MagicMock) -> None: mock_pytest_run.return_value = "PASSED ::test_1::test_1\nPASSED ::test_2::test_2\nPASSED ::test_3::test_3" # Act & Assert - with self.assertRaises(CheckError): + with pytest.raises(CheckError): max_score_exceeded_check.run() @patch("grader.checks.run_tests_check.RunTestsCheck._RunTestsCheck__pytest_run") - def test_04_logs_correct_passed_and_failed_counts(self, mock_pytest_run: MagicMock) -> None: + def test_04_logs_correct_passed_and_failed_counts( + self, + mock_pytest_run: MagicMock, + log_output: Callable[[], list[str]], + ) -> None: """Verify run logs the correct number of passed and failed tests.""" # Arrange mock_pytest_run.return_value = "PASSED ::test_1::test_1\nFAILED ::test_2::test_2" # Act - with self.assertLogs("grader", level=VERBOSE) as log: - self.tests_check.run() + self.tests_check.run() # Assert - self.assertIn("VERBOSE:grader:Passed tests: 1/2", log.output) - self.assertIn("VERBOSE:grader:Failed tests: 1/2", log.output) + assert "VERBOSE:grader:Passed tests: 1/2" in log_output() + assert "VERBOSE:grader:Failed tests: 1/2" in log_output() @patch("grader.checks.run_tests_check.RunTestsCheck._RunTestsCheck__pytest_run") def test_05_empty_test_results(self, mock_pytest_run: MagicMock) -> None: @@ -160,7 +161,7 @@ def test_05_empty_test_results(self, mock_pytest_run: MagicMock) -> None: score = self.tests_check.run() # Assert - self.assertEqual(score, expected_score) + assert score == expected_score @patch("grader.checks.run_tests_check.RunTestsCheck._RunTestsCheck__pytest_run") def test_06_invalid_pytest_output(self, mock_pytest_run: MagicMock) -> None: @@ -173,7 +174,7 @@ def test_06_invalid_pytest_output(self, mock_pytest_run: MagicMock) -> None: score = self.tests_check.run() # Assert - self.assertEqual(score, expected_score) + assert score == expected_score @patch("grader.utils.process.run") def test_07_pytest_raises_os_error(self, mock_run: MagicMock) -> None: @@ -182,7 +183,7 @@ def test_07_pytest_raises_os_error(self, mock_run: MagicMock) -> None: mock_run.side_effect = OSError("Test error") # Act & Assert - with self.assertRaises(CheckError): + with pytest.raises(CheckError): self.tests_check.run() @patch("grader.utils.process.run") @@ -192,7 +193,7 @@ def test_08_pytest_raises_value_error(self, mock_run: MagicMock) -> None: mock_run.side_effect = ValueError("Test error") # Act & Assert - with self.assertRaises(CheckError): + with pytest.raises(CheckError): self.tests_check.run() @patch("grader.utils.process.run") @@ -202,7 +203,7 @@ def test_09_pytest_returncode_greater_than_2(self, mock_run: MagicMock) -> None: mock_run.return_value.returncode = 3 # Act & Assert - with self.assertRaises(CheckError): + with pytest.raises(CheckError): self.tests_check.run() @patch("grader.checks.run_tests_check.RunTestsCheck._RunTestsCheck__pytest_run") @@ -218,7 +219,7 @@ def test_10_class_scored(self, mock_pytest_run: MagicMock) -> None: score = self.tests_check.run() # Assert - self.assertEqual(score, expected_score) + assert score == expected_score @patch("grader.checks.run_tests_check.RunTestsCheck._RunTestsCheck__pytest_run") def test_11_test_scored_both_name_and_class(self, mock_pytest_run: MagicMock) -> None: @@ -242,7 +243,7 @@ def test_11_test_scored_both_name_and_class(self, mock_pytest_run: MagicMock) -> score = tests_check.run() # Assert - self.assertEqual(score, expected_score) + assert score == expected_score @patch("grader.checks.run_tests_check.RunTestsCheck._RunTestsCheck__pytest_run") def test_12_test_default_scored(self, mock_pytest_run: MagicMock) -> None: @@ -265,4 +266,4 @@ def test_12_test_default_scored(self, mock_pytest_run: MagicMock) -> None: score = tests_check.run() # Assert - self.assertEqual(score, expected_score) + assert score == expected_score diff --git a/tests/unit/test_type_hints_check.py b/tests/unit/test_type_hints_check.py index 5426399..d00bf5e 100644 --- a/tests/unit/test_type_hints_check.py +++ b/tests/unit/test_type_hints_check.py @@ -1,25 +1,26 @@ """Unit tests for the TypeHintsCheck class.""" -import os import shutil -import unittest +from collections.abc import Callable +from pathlib import Path from subprocess import CompletedProcess from unittest.mock import MagicMock, patch +import pytest + import grader.utils.constants as const from grader.checks.type_hints_check import TypeHintsCheck from grader.exceptions import CheckError from grader.models.check_result import ScoredCheckResult -class TestTypeHintsCheck(unittest.TestCase): +class TestTypeHintsCheck: """Test cases for the TypeHintsCheck class.""" - def setUp(self) -> None: + def setup_method(self) -> None: """Set up the test environment.""" self.type_hints_check = TypeHintsCheck("type_hints", "sample_dir", 2, is_venv_required=False) # This way, we have 3 ranges: 0-33, 34-66, 67-100 - return super().setUp() @patch("grader.utils.process.run") @patch("grader.utils.files.find_all_source_files") @@ -37,8 +38,8 @@ def test_01_mypy_called(self, mocked_find_python_files: MagicMock, mocked_run: M mocked_run.return_value = CompletedProcess(args=["mypy"], returncode=0) # Copy mypy_sample_linecount.txt to the reports directory - os.makedirs(const.REPORTS_TEMP_DIR, exist_ok=True) - shutil.copy(os.path.join("tests", "mypy_sample_linecount.txt"), const.MYPY_LINE_COUNT_REPORT) + Path(const.REPORTS_TEMP_DIR).mkdir(parents=True, exist_ok=True) + shutil.copy(str(Path("tests", "mypy_sample_linecount.txt")), const.MYPY_LINE_COUNT_REPORT) # Act self.type_hints_check.run() @@ -48,11 +49,11 @@ def test_01_mypy_called(self, mocked_find_python_files: MagicMock, mocked_run: M mocked_find_python_files.assert_called_once() mocked_run.assert_called_once() # called_with[0] is *args. First arg is the list of "args" of lint.Run - self.assertIn("file1.py", called_with[0][0]) - self.assertIn("file2.py", called_with[0][0]) + assert "file1.py" in called_with[0][0] + assert "file2.py" in called_with[0][0] @patch("grader.utils.process.run") - def test_03_mypy_report_missing(self, mocked_run: MagicMock) -> None: + def test_03_mypy_report_missing(self, mocked_run: MagicMock, log_output: Callable[[], list[str]]) -> None: """ Test if an error is logged when the mypy linecount report is missing. @@ -62,17 +63,16 @@ def test_03_mypy_report_missing(self, mocked_run: MagicMock) -> None: # Arrange mocked_run.return_value = CompletedProcess(args=["mypy"], returncode=0) - if os.path.exists(const.MYPY_LINE_COUNT_REPORT): - os.remove(const.MYPY_LINE_COUNT_REPORT) + if Path(const.MYPY_LINE_COUNT_REPORT).exists(): + Path(const.MYPY_LINE_COUNT_REPORT).unlink() # Act - with self.assertLogs(level="ERROR") as cm: - with self.assertRaises(CheckError): - self.type_hints_check.run() - is_error_logged = any("Mypy linecount report not found" in log for log in cm.output) + with pytest.raises(CheckError): + self.type_hints_check.run() + is_error_logged = any("Mypy linecount report not found" in log for log in log_output()) # Assert - self.assertTrue(is_error_logged) + assert is_error_logged @patch("grader.utils.process.run") def test_04_translate_score_zero(self, mocked_run: MagicMock) -> None: @@ -93,7 +93,7 @@ def test_04_translate_score_zero(self, mocked_run: MagicMock) -> None: actual_score = self.type_hints_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("grader.utils.process.run") def test_05_translate_score_inside_first_range(self, mocked_run: MagicMock) -> None: @@ -114,7 +114,7 @@ def test_05_translate_score_inside_first_range(self, mocked_run: MagicMock) -> N actual_score = self.type_hints_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("grader.utils.process.run") def test_06_translate_score_right_bound_first_range(self, mocked_run: MagicMock) -> None: @@ -135,7 +135,7 @@ def test_06_translate_score_right_bound_first_range(self, mocked_run: MagicMock) actual_score = self.type_hints_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("grader.utils.process.run") def test_07_translate_score_left_bound_second_range(self, mocked_run: MagicMock) -> None: @@ -156,7 +156,7 @@ def test_07_translate_score_left_bound_second_range(self, mocked_run: MagicMock) actual_score = self.type_hints_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("grader.utils.process.run") def test_08_translate_score_inside_bound_second_range(self, mocked_run: MagicMock) -> None: @@ -177,7 +177,7 @@ def test_08_translate_score_inside_bound_second_range(self, mocked_run: MagicMoc actual_score = self.type_hints_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("grader.utils.process.run") def test_09_translate_score_right_bound_second_range(self, mocked_run: MagicMock) -> None: @@ -198,7 +198,7 @@ def test_09_translate_score_right_bound_second_range(self, mocked_run: MagicMock actual_score = self.type_hints_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("grader.utils.process.run") def test_10_translate_score_inside_bound_third_range(self, mocked_run: MagicMock) -> None: @@ -219,7 +219,7 @@ def test_10_translate_score_inside_bound_third_range(self, mocked_run: MagicMock actual_score = self.type_hints_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("grader.utils.process.run") def test_11_translate_score_max(self, mocked_run: MagicMock) -> None: @@ -240,7 +240,7 @@ def test_11_translate_score_max(self, mocked_run: MagicMock) -> None: actual_score = self.type_hints_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score @patch("grader.utils.process.run") def test_12_translate_score_all_zeros(self, mocked_run: MagicMock) -> None: @@ -261,4 +261,4 @@ def test_12_translate_score_all_zeros(self, mocked_run: MagicMock) -> None: actual_score = self.type_hints_check.run() # Assert - self.assertEqual(expected_score, actual_score) + assert expected_score == actual_score diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 0d9624e..ea76d05 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -1,45 +1,40 @@ """Unit tests for the desktop.utils module.""" -import unittest import uuid -from unittest.mock import patch +from unittest.mock import MagicMock, patch from desktop.utils import extract_student_id_from_path -class TestExtractStudentIdFromPath(unittest.TestCase): +class TestExtractStudentIdFromPath: """Unit tests for the extract_student_id_from_path function.""" def test_01_extracts_id_from_valid_submission_path(self) -> None: """Verify the student id is extracted from a Moodle-style submission path.""" path = "root/hw1/12345-John_Doe_1_assignsubmission_file/submission.zip" - self.assertEqual(extract_student_id_from_path(path), "12345") + assert extract_student_id_from_path(path) == "12345" def test_02_extracts_id_with_uppercase_letters(self) -> None: """Verify an id made of digits and uppercase letters is extracted in full.""" path = "root/hw1/AB12C-Jane_Doe_2_assignsubmission_file/submission.zip" - self.assertEqual(extract_student_id_from_path(path), "AB12C") + assert extract_student_id_from_path(path) == "AB12C" @patch("desktop.utils.uuid.uuid4") - def test_03_no_dash_subfolder_falls_back_to_uuid(self, mock_uuid4: unittest.mock.MagicMock) -> None: + def test_03_no_dash_subfolder_falls_back_to_uuid(self, mock_uuid4: MagicMock) -> None: """Verify a path without the '-name/file' shape falls back to a random uuid.""" mock_uuid4.return_value = uuid.UUID("11111111-1111-1111-1111-111111111111") result = extract_student_id_from_path("some/plain/path") - self.assertEqual(result, "11111111-1111-1111-1111-111111111111") + assert result == "11111111-1111-1111-1111-111111111111" @patch("desktop.utils.uuid.uuid4") - def test_04_empty_id_group_falls_back_to_uuid(self, mock_uuid4: unittest.mock.MagicMock) -> None: + def test_04_empty_id_group_falls_back_to_uuid(self, mock_uuid4: MagicMock) -> None: """Verify a path matching the shape but with no id characters before the dash falls back to a uuid.""" mock_uuid4.return_value = uuid.UUID("22222222-2222-2222-2222-222222222222") result = extract_student_id_from_path("root/hw1/-Name_1_assignsubmission_file/submission.zip") - self.assertEqual(result, "22222222-2222-2222-2222-222222222222") - - -if __name__ == "__main__": - unittest.main() + assert result == "22222222-2222-2222-2222-222222222222" diff --git a/tests/unit/test_virtual_environment.py b/tests/unit/test_virtual_environment.py index 09b2b27..3351d51 100644 --- a/tests/unit/test_virtual_environment.py +++ b/tests/unit/test_virtual_environment.py @@ -3,10 +3,12 @@ import os import shutil import subprocess -import unittest +from pathlib import Path from subprocess import CompletedProcess from unittest.mock import MagicMock, patch +import pytest + import grader.utils.constants as const from grader.exceptions import VirtualEnvironmentError from grader.utils.process import run @@ -14,67 +16,52 @@ # TODO - These tests take some time to run, also aren't exactly unit tests. Consider refactoring -class TestsVirtualEnvironment(unittest.TestCase): +class TestsVirtualEnvironment: """Test cases for the VirtualEnvironment class.""" - def __init__(self, methodName: str = "runTest") -> None: - """ - Initialize the test case. - - :param methodName: The name of the test method to run. - :type methodName: str - """ + def setup_method(self) -> None: + """Set up the test environment.""" self.__sample_root_dir_path = "sample_root_dir" self.__sample_package_name = "pylint" self.__sample_package_version = "4.0.8" - super().__init__(methodName) + Path(self.__sample_root_dir_path).mkdir(parents=True, exist_ok=True) - def setUp(self) -> None: - """Set up the test environment.""" - os.makedirs(self.__sample_root_dir_path, exist_ok=True) - - return super().setUp() - - def tearDown(self) -> None: + def teardown_method(self) -> None: """Tear down the test environment.""" - if os.path.exists(self.__sample_root_dir_path): + if Path(self.__sample_root_dir_path).exists(): shutil.rmtree(self.__sample_root_dir_path) - return super().tearDown() - def test_01_existing_venv(self) -> None: """Verify that the VirtualEnvironment class cleans pre-existing virtual environments.""" # Arrange - possible_paths = [ - os.path.join(self.__sample_root_dir_path, venv_path) for venv_path in const.POSSIBLE_VENV_DIRS - ] + possible_paths = [str(Path(self.__sample_root_dir_path, venv_path)) for venv_path in const.POSSIBLE_VENV_DIRS] for path in possible_paths: - os.makedirs(path, exist_ok=True) + Path(path).mkdir(parents=True, exist_ok=True) # Act with VirtualEnvironment(self.__sample_root_dir_path): - do_directories_exist = (os.path.exists(venv_path) for venv_path in possible_paths) + do_directories_exist = (Path(venv_path).exists() for venv_path in possible_paths) # Assert - self.assertTrue(not any(do_directories_exist)) + assert not any(do_directories_exist) def test_03_successful_venv_creation(self) -> None: """Verify that the VirtualEnvironment class can successfully create a virtual environment.""" # Arrange - unix_path_to_python = os.path.join(self.__sample_root_dir_path, const.VENV_NAME, "bin", const.PYTHON_BIN_UNIX) - windows_path_to_python = os.path.join( - self.__sample_root_dir_path, const.VENV_NAME, "Scripts", const.PYTHON_BIN_WINDOWS + unix_path_to_python = str(Path(self.__sample_root_dir_path, const.VENV_NAME, "bin", const.PYTHON_BIN_UNIX)) + windows_path_to_python = str( + Path(self.__sample_root_dir_path, const.VENV_NAME, "Scripts", const.PYTHON_BIN_WINDOWS) ) path_to_python = windows_path_to_python if os.name == "nt" else unix_path_to_python # Act with VirtualEnvironment(self.__sample_root_dir_path): - does_python_exist = os.path.exists(path_to_python) + does_python_exist = Path(path_to_python).exists() # Assert - self.assertTrue(does_python_exist) + assert does_python_exist @patch("subprocess.run") def test_04_failed_venv_creation(self, patched_run: MagicMock) -> None: @@ -87,16 +74,16 @@ def test_04_failed_venv_creation(self, patched_run: MagicMock) -> None: patched_run.return_value = subprocess.CompletedProcess([], 1) # Act & Assert - with self.assertRaises(VirtualEnvironmentError), VirtualEnvironment(self.__sample_root_dir_path): + with pytest.raises(VirtualEnvironmentError), VirtualEnvironment(self.__sample_root_dir_path): pass def test_05_install_requirements(self) -> None: """Verify that the VirtualEnvironment class installs the requirements.""" # Arrange - requirements_path = os.path.join(self.__sample_root_dir_path, "requirements.txt") + requirements_path = str(Path(self.__sample_root_dir_path, "requirements.txt")) self.__create_sample_requirements(requirements_path) - pip_full_path = os.path.join(self.__sample_root_dir_path, const.VENV_NAME, const.PIP_PATH) + pip_full_path = str(Path(self.__sample_root_dir_path, const.VENV_NAME, const.PIP_PATH)) # Act with VirtualEnvironment(self.__sample_root_dir_path): @@ -109,8 +96,8 @@ def test_05_install_requirements(self) -> None: is_version_correct = self.__sample_package_version in pip_run_result.stdout # Assert - self.assertTrue(is_expected_package_installed) - self.assertTrue(is_version_correct) + assert is_expected_package_installed + assert is_version_correct @patch("subprocess.run") def test_06_fail_install_requirements(self, patched_run: MagicMock) -> None: @@ -130,13 +117,13 @@ def custom_run_behavior(command: list[str], *args: list, **kwargs: dict) -> Comp patched_run.side_effect = custom_run_behavior # Act & Assert - with self.assertRaises(VirtualEnvironmentError), VirtualEnvironment(self.__sample_root_dir_path): + with pytest.raises(VirtualEnvironmentError), VirtualEnvironment(self.__sample_root_dir_path): pass def test_07_install_grader_requirements(self) -> None: """Verify that the VirtualEnvironment class installs the grader requirements.""" # Arrange - pip_full_path = os.path.join(self.__sample_root_dir_path, const.VENV_NAME, const.PIP_PATH) + pip_full_path = str(Path(self.__sample_root_dir_path, const.VENV_NAME, const.PIP_PATH)) # Act with VirtualEnvironment(self.__sample_root_dir_path): @@ -146,8 +133,8 @@ def test_07_install_grader_requirements(self) -> None: is_version_correct = "7.16.0" in pip_run_result.stdout # Assert - self.assertTrue(is_expected_package_installed) - self.assertTrue(is_version_correct) + assert is_expected_package_installed + assert is_version_correct @patch("subprocess.run") def test_08_fail_install_grader_requirements(self, patched_run: MagicMock) -> None: @@ -167,23 +154,23 @@ def custom_run_behavior(command: list[str], *args: list, **kwargs: dict) -> Comp patched_run.side_effect = custom_run_behavior # Act & Assert - with self.assertRaises(VirtualEnvironmentError), VirtualEnvironment(self.__sample_root_dir_path): + with pytest.raises(VirtualEnvironmentError), VirtualEnvironment(self.__sample_root_dir_path): pass def test_09_teardown(self) -> None: """Verify that the VirtualEnvironment class removes the virtual environment when the context manager is exited.""" # Arrange - venv_path = os.path.join(self.__sample_root_dir_path, const.VENV_NAME) + venv_path = str(Path(self.__sample_root_dir_path, const.VENV_NAME)) # Act with VirtualEnvironment(self.__sample_root_dir_path): - does_venv_exist_before = os.path.exists(venv_path) + does_venv_exist_before = Path(venv_path).exists() - does_venv_exist_after = os.path.exists(venv_path) + does_venv_exist_after = Path(venv_path).exists() # Assert - self.assertTrue(does_venv_exist_before) - self.assertFalse(does_venv_exist_after) + assert does_venv_exist_before + assert not does_venv_exist_after def __create_sample_requirements(self, requirements_path: str) -> None: """ diff --git a/uv.lock b/uv.lock index 6bd4c3f..9efe428 100644 --- a/uv.lock +++ b/uv.lock @@ -1,5 +1,5 @@ version = 1 -revision = 3 +revision = 5 requires-python = ">=3.12" resolution-markers = [ "python_full_version >= '3.15'", @@ -1027,9 +1027,10 @@ wheels = [ [[package]] name = "pygrader" -version = "2.0.0" +version = "2.1.0" source = { editable = "." } dependencies = [ + { name = "complexipy" }, { name = "dotenv" }, { name = "py-cove" }, { name = "pylint" }, @@ -1040,10 +1041,10 @@ dependencies = [ [package.dev-dependencies] dev = [ - { name = "complexipy" }, { name = "coverage" }, { name = "mypy" }, { name = "myst-parser" }, + { name = "pytest" }, { name = "ruff" }, { name = "sphinx" }, { name = "sphinx-autodoc2" }, @@ -1061,6 +1062,7 @@ grader-runtime = [ [package.metadata] requires-dist = [ + { name = "complexipy", specifier = ">=8.0.0" }, { name = "dotenv", specifier = ">=0.9.9" }, { name = "py-cove", git = "https://github.com/lyubolp/py-cove?rev=v1.4.0" }, { name = "pylint", specifier = ">=4.0.8" }, @@ -1071,10 +1073,10 @@ requires-dist = [ [package.metadata.requires-dev] dev = [ - { name = "complexipy", specifier = ">=8.0.0" }, { name = "coverage", specifier = ">=7.16.0" }, { name = "mypy", specifier = ">=2.3.1" }, { name = "myst-parser", specifier = ">=5.1.0" }, + { name = "pytest", specifier = ">=9.1.1" }, { name = "ruff", specifier = ">=0.16.6" }, { name = "sphinx", specifier = ">=9.1.0" }, { name = "sphinx-autodoc2", specifier = ">=0.4.2" },