@@ -33,12 +33,48 @@ So I guess it's okay if we have a few guidelines. The [Google Python Style Guide
...
@@ -33,12 +33,48 @@ So I guess it's okay if we have a few guidelines. The [Google Python Style Guide
# Testing
# Testing
As much as possible, we should write automatic tests for our code using `pytest`
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.
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`.
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:
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
defsay_hello(user):
ifnotisinstance(user,str):
raiseTypeError("The 'user' argument must be a string.")
greeting=(f'Hello {user}!')
returngreeting
```
This function simply receives a string and returns another string greeting the user.
A `test_examples.py` should contain the test for it:
```python
deftest_say_hello():
""" Checks default behaviour """
user="Alice"
expected_output="Hello Alice!"
result=qimex.examples.say_hello(user)
assertresult==expected_output
```
This is simply running the function and checking if the result is as expected.
Let's say you have a project
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.