Skip to content

Commit 0188fb8

Browse files
committed
Merge branch 'master' into cli-migrate-py_unittest
2 parents ffa9cd3 + e0ab729 commit 0188fb8

File tree

9 files changed

+52
-12
lines changed

9 files changed

+52
-12
lines changed

cli/casp/README.md

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ For example, to add a `my-command` command, follow these steps:
4848

4949
import click
5050

51-
from .commands import hi
52-
from .commands import version
53-
from .commands import my_command # Add this line
51+
from casp.commands import hi
52+
from casp.commands import version
53+
from casp.commands import my_command # Add this line
5454

5555
@click.group()
5656
def cli():
@@ -69,5 +69,45 @@ Once you have completed these steps, the new command will be available as
6969
To run all unit tests for the `casp` CLI, use the following command from the root of the project:
7070

7171
```bash
72-
python -m unittest discover -s cli/casp/src/casp/tests -v
72+
python -m unittest discover -s cli/casp/src/casp/tests -p '*_test.py' -v
73+
```
74+
75+
## Writing Tests
76+
77+
To add tests for a new command or feature, create a new Python file in the
78+
`cli/casp/src/casp/tests` directory. The file name should follow the pattern
79+
`<feature_name>_test.py`.
80+
81+
Each test file should contain test classes that inherit from `unittest.TestCase`.
82+
You can use `click.testing.CliRunner` to invoke your CLI commands in tests.
83+
84+
Here's an example for a `my-command` feature:
85+
86+
```python
87+
# cli/casp/src/casp/tests/test_my_command.py
88+
89+
import unittest
90+
from click.testing import CliRunner
91+
92+
from casp.commands import my_command
93+
94+
class MyCommandTest(unittest.TestCase):
95+
"""Tests for the `my-command` CLI command."""
96+
97+
def setUp(self):
98+
self.runner = CliRunner()
99+
100+
def test_my_command_no_verbose(self):
101+
"""Test `my-command` without the --verbose flag."""
102+
result = self.runner.invoke(my_command.cli)
103+
self.assertEqual(result.exit_code, 0)
104+
self.assertIn('This is my new command.', result.output)
105+
self.assertNotIn('Verbose logging is enabled.', result.output)
106+
107+
def test_my_command_verbose(self):
108+
"""Test `my-command` with the --verbose flag."""
109+
result = self.runner.invoke(my_command.cli, ['--verbose'])
110+
self.assertEqual(result.exit_code, 0)
111+
self.assertIn('Verbose logging is enabled.', result.output)
112+
self.assertIn('This is my new command.', result.output)
73113
```

cli/casp/src/casp/tests/commands/test_format.py renamed to cli/casp/src/casp/tests/commands/format_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""Tests for the format command.
1515
1616
For running all the tests, use (from the root of the project):
17-
python -m unittest discover -s cli/casp/src/casp/tests -p test_format.py -v
17+
python -m unittest discover -s cli/casp/src/casp/tests -p format_test.py -v
1818
"""
1919

2020
from pathlib import Path

cli/casp/src/casp/tests/commands/test_init.py renamed to cli/casp/src/casp/tests/commands/init_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""Tests for the init command.
1515
1616
For running all the tests, use (from the root of the project):
17-
python -m unittest discover -s cli/casp/src/casp/tests -p test_init.py -v
17+
python -m unittest discover -s cli/casp/src/casp/tests -p init_test.py -v
1818
"""
1919

2020
import os

cli/casp/src/casp/tests/commands/test_lint.py renamed to cli/casp/src/casp/tests/commands/lint_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""Tests for the lint command.
1515
1616
For running all the tests, use (from the root of the project):
17-
python -m unittest discover -s cli/casp/src/casp/tests -p test_lint.py -v
17+
python -m unittest discover -s cli/casp/src/casp/tests -p lint_test.py -v
1818
"""
1919

2020
from pathlib import Path

cli/casp/src/casp/tests/commands/test_reproduce.py renamed to cli/casp/src/casp/tests/commands/reproduce_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""Tests for the reproduce command.
1515
1616
For running all the tests, use (from the root of the project):
17-
python -m unittest discover -s cli/casp/src/casp/tests -p test_reproduce.py -v
17+
python -m unittest discover -s cli/casp/src/casp/tests -p reproduce_test.py -v
1818
"""
1919

2020
from pathlib import Path

cli/casp/src/casp/tests/utils/test_config.py renamed to cli/casp/src/casp/tests/utils/config_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""Tests for config utility functions."
1515
1616
For running all the tests, use (from the root of the project):
17-
python -m unittest discover -s cli/casp/src/casp/tests -p test_config.py -v
17+
python -m unittest discover -s cli/casp/src/casp/tests -p config_test.py -v
1818
"""
1919

2020
import json

cli/casp/src/casp/tests/utils/test_docker.py renamed to cli/casp/src/casp/tests/utils/docker_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""Tests for docker utility functions.
1515
1616
For running, use (from the root of the project):
17-
python -m unittest discover -s cli/casp/src/casp/tests -p test_docker.py -v
17+
python -m unittest discover -s cli/casp/src/casp/tests -p docker_test.py -v
1818
"""
1919

2020
import os

cli/casp/src/casp/tests/utils/test_gcloud.py renamed to cli/casp/src/casp/tests/utils/gcloud_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""Tests for the gcloud utility functions.
1515
1616
For running, use (from the root of the project):
17-
python -m unittest discover -s cli/casp/src/casp/tests -p test_gcloud.py -v
17+
python -m unittest discover -s cli/casp/src/casp/tests -p gcloud_test.py -v
1818
"""
1919

2020
import subprocess

cli/casp/src/casp/tests/utils/test_local_butler.py renamed to cli/casp/src/casp/tests/utils/local_butler_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
For running, use (from the root of the project):
1717
python -m unittest discover -s cli/casp/src/casp/tests
18-
-p test_local_butler.py -v
18+
-p local_butler_test.py -v
1919
"""
2020

2121
from pathlib import Path

0 commit comments

Comments
 (0)