- Replaced deprecated typing.List/Dict/Tuple with native list/dict/tuple - Fixed trailing whitespace issues - Fixed blank line whitespace issues - Removed unused variables and imports - Applied black formatting
139 lines
4.3 KiB
Python
139 lines
4.3 KiB
Python
"""Tests for the CLI module."""
|
|
|
|
import pytest
|
|
from click.testing import CliRunner
|
|
from nl2gherkin.cli.commands import cli, convert, validate
|
|
|
|
|
|
class TestCLI:
|
|
"""Test cases for CLI commands."""
|
|
|
|
@pytest.fixture
|
|
def runner(self):
|
|
"""Create a CLI runner."""
|
|
return CliRunner()
|
|
|
|
def test_cli_help(self, runner):
|
|
"""Test CLI help command."""
|
|
result = runner.invoke(cli, ["--help"])
|
|
|
|
assert result.exit_code == 0
|
|
assert "convert" in result.output
|
|
assert "interactive" in result.output
|
|
assert "validate" in result.output
|
|
|
|
def test_convert_help(self, runner):
|
|
"""Test convert command help."""
|
|
result = runner.invoke(convert, ["--help"])
|
|
|
|
assert result.exit_code == 0
|
|
assert "INPUT_FILE" in result.output
|
|
assert "--output" in result.output
|
|
assert "--framework" in result.output
|
|
assert "--validate" in result.output
|
|
|
|
def test_convert_requires_input_file(self, runner):
|
|
"""Test that convert requires an input file."""
|
|
result = runner.invoke(convert, [])
|
|
|
|
assert result.exit_code != 0
|
|
assert "INPUT_FILE" in result.output or "Missing argument" in result.output
|
|
|
|
def test_validate_help(self, runner):
|
|
"""Test validate command help."""
|
|
result = runner.invoke(validate, ["--help"])
|
|
|
|
assert result.exit_code == 0
|
|
assert "GHERKIN_FILE" in result.output
|
|
|
|
def test_convert_with_sample_requirement(self, runner, tmp_path):
|
|
"""Test converting a sample requirement file."""
|
|
req_file = tmp_path / "requirements.txt"
|
|
req_file.write_text("As a user, I want to login so that I can access my account")
|
|
|
|
result = runner.invoke(convert, [str(req_file), "--no-validate"])
|
|
|
|
assert result.exit_code == 0
|
|
assert "Feature:" in result.output
|
|
assert "Given" in result.output
|
|
assert "When" in result.output
|
|
assert "Then" in result.output
|
|
|
|
def test_convert_with_output_file(self, runner, tmp_path):
|
|
"""Test converting with output file option."""
|
|
req_file = tmp_path / "requirements.txt"
|
|
req_file.write_text("As a user, I want to search for products")
|
|
|
|
output_file = tmp_path / "output.feature"
|
|
|
|
result = runner.invoke(
|
|
convert,
|
|
[
|
|
str(req_file),
|
|
"--output",
|
|
str(output_file),
|
|
"--no-validate",
|
|
],
|
|
)
|
|
|
|
assert result.exit_code == 0
|
|
assert output_file.exists()
|
|
|
|
content = output_file.read_text()
|
|
assert "Feature:" in content
|
|
|
|
def test_convert_with_different_frameworks(self, runner, tmp_path):
|
|
"""Test converting with different BDD frameworks."""
|
|
req_file = tmp_path / "requirements.txt"
|
|
req_file.write_text("As a user, I want to login")
|
|
|
|
for framework in ["cucumber", "behave", "pytest-bdd"]:
|
|
result = runner.invoke(
|
|
convert,
|
|
[
|
|
str(req_file),
|
|
"--framework",
|
|
framework,
|
|
"--no-validate",
|
|
],
|
|
)
|
|
|
|
assert result.exit_code == 0, f"Failed for framework: {framework}"
|
|
|
|
def test_validate_valid_gherkin(self, runner, tmp_path):
|
|
"""Test validating a valid Gherkin file."""
|
|
gherkin_file = tmp_path / "test.feature"
|
|
gherkin_file.write_text("""Feature: Test
|
|
Scenario: A test scenario
|
|
Given a setup
|
|
When an action occurs
|
|
Then an expected result
|
|
""")
|
|
|
|
result = runner.invoke(validate, [str(gherkin_file)])
|
|
|
|
assert result.exit_code == 0
|
|
|
|
def test_ambiguity_check_flag(self, runner, tmp_path):
|
|
"""Test that ambiguity check flag produces warnings."""
|
|
req_file = tmp_path / "requirements.txt"
|
|
req_file.write_text("As a user, I want to do something with some data")
|
|
|
|
result = runner.invoke(
|
|
convert,
|
|
[
|
|
str(req_file),
|
|
"--ambiguity-check",
|
|
"--no-validate",
|
|
],
|
|
)
|
|
|
|
assert result.exit_code == 0
|
|
|
|
def test_interactive_command_exists(self, runner):
|
|
"""Test that interactive command exists."""
|
|
result = runner.invoke(cli, ["interactive", "--help"])
|
|
|
|
assert result.exit_code == 0
|
|
assert "--framework" in result.output
|