| ... | ... | @@ -33,12 +33,48 @@ So I guess it's okay if we have a few guidelines. The [Google Python Style Guide |
|
|
|
# Testing
|
|
|
|
As much as possible, we should write automatic tests for our code using `pytest`
|
|
|
|
|
|
|
|
Tests should be located in a `tests` directory, at the root of your repository.
|
|
|
|
|
|
|
|
In this directory, create a file for each module of your package, name following the convention `test_modulename.py`.
|
|
|
|
Tests should be located in a `tests` directory, at the root of your repository. In this directory, create a file for each module of your package, name following the convention `test_modulename.py`.
|
|
|
|
|
|
|
|
For example, your project structure could be like this:
|
|
|
|
|
|
|
|
```
|
|
|
|
example-repo/
|
|
|
|
┣ qimex/
|
|
|
|
┃ ┣ __init__.py
|
|
|
|
┃ ┗ examples.py
|
|
|
|
┣ tests/
|
|
|
|
┃ ┣ __init__.py
|
|
|
|
┃ ┗ test_examples.py
|
|
|
|
┣ .gitignore
|
|
|
|
┣ LICENSE
|
|
|
|
┗ README.md
|
|
|
|
```
|
|
|
|
|
|
|
|
Inside `examples.py` you could have this function:
|
|
|
|
|
|
|
|
```python
|
|
|
|
def say_hello(user):
|
|
|
|
|
|
|
|
if not isinstance(user, str):
|
|
|
|
raise TypeError("The 'user' argument must be a string.")
|
|
|
|
|
|
|
|
greeting = (f'Hello {user}!')
|
|
|
|
|
|
|
|
return greeting
|
|
|
|
```
|
|
|
|
This function simply receives a string and returns another string greeting the user.
|
|
|
|
|
|
|
|
A `test_examples.py` should contain the test for it:
|
|
|
|
|
|
|
|
```python
|
|
|
|
def test_say_hello():
|
|
|
|
""" Checks default behaviour """
|
|
|
|
user = "Alice"
|
|
|
|
expected_output = "Hello Alice!"
|
|
|
|
result = qimex.examples.say_hello(user)
|
|
|
|
assert result == expected_output
|
|
|
|
```
|
|
|
|
|
|
|
|
This is simply running the function and checking if the result is as expected.
|
|
|
|
|
|
|
|
Let's say you have a project |
|
|
\ No newline at end of file |
|
|
|
To run all the tests, navigate to the root of your project and run `pytest`. Everything inside the `tests` folder will be executed, and a report will be shown. |
|
|
\ No newline at end of file |