[TESTING.md] Enhance TESTING docs (#2611)
* Update TESTS.md * Update TESTS.md * Update TESTS.md * Start of TOOLS.md * Finished writing VS Code part * Added Pycharm docs & relative img links * Update TOOLS.md * Added Spyder Docs * Added VENV docs * Added conda environments docs * Wrote Windows tutorial for adding to path * Improved shared test file and added disclaimer to TOOLS.md * Added docs for JupyterLab and Sublime Text * Added Virtualenvwrapper piece. * Cleaned up. * Updated image paths * Fixed Images x2 * Images have absolute path * Update docs/TESTS.md Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com> * Update docs/TESTS.md Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com> * Update docs/TESTS.md Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com> * Update docs/TESTS.md Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com> * Update docs/TESTS.md Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com> * Update docs/TESTS.md Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com> * Update docs/TOOLS.md Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com> * Update docs/TOOLS.md Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com> * Update docs/TESTS.md Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com> * Update docs/TOOLS.md Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com> * Update docs/TOOLS.md Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com> * Update exercises/shared/.docs/tests.md Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com> * Update exercises/shared/.docs/tests.md Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com> * Fixed capitalization * Update docs/TOOLS.md Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com> * Fixed language Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com> * Update docs/TOOLS.md Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com> * Update docs/TOOLS.md * Update docs/TOOLS.md * Update docs/TESTS.md * Update docs/TOOLS.md * Update docs/TOOLS.md * Update docs/TESTS.md Co-authored-by: Mukesh Gurpude <55982424+mukeshgurpude@users.noreply.github.com> * Update docs/TESTS.md Co-authored-by: Mukesh Gurpude <55982424+mukeshgurpude@users.noreply.github.com> * Update docs/TESTS.md Co-authored-by: Mukesh Gurpude <55982424+mukeshgurpude@users.noreply.github.com> * Update docs/TOOLS.md Co-authored-by: Mukesh Gurpude <55982424+mukeshgurpude@users.noreply.github.com> * Update docs/TESTS.md Co-authored-by: ynfle <23086821+ynfle@users.noreply.github.com> * Added pytest.ini docs * Update docs/TESTS.md Co-authored-by: BethanyG <BethanyG@users.noreply.github.com> * Update docs/TESTS.md Co-authored-by: BethanyG <BethanyG@users.noreply.github.com> * Update docs/TESTS.md Co-authored-by: BethanyG <BethanyG@users.noreply.github.com> * Update docs/TOOLS.md Co-authored-by: BethanyG <BethanyG@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: BethanyG <BethanyG@users.noreply.github.com> * Update docs/TOOLS.md Co-authored-by: BethanyG <BethanyG@users.noreply.github.com> * Update docs/TOOLS.md Co-authored-by: BethanyG <BethanyG@users.noreply.github.com> * Update docs/TOOLS.md Co-authored-by: BethanyG <BethanyG@users.noreply.github.com> * Update docs/TOOLS.md Co-authored-by: BethanyG <BethanyG@users.noreply.github.com> * Update headers and links Co-authored-by: Isaac Good <IsaacG@users.noreply.github.com> Co-authored-by: Mukesh Gurpude <55982424+mukeshgurpude@users.noreply.github.com> Co-authored-by: ynfle <23086821+ynfle@users.noreply.github.com> Co-authored-by: BethanyG <BethanyG@users.noreply.github.com>
232
docs/TESTS.md
@@ -1,115 +1,211 @@
|
||||
# Tests
|
||||
|
||||
## Tools to install
|
||||
- [Tests](#tests)
|
||||
- [Pytest](#pytest)
|
||||
- [Installing pytest Globally](#installing-pytest-globally)
|
||||
- [Windows](#windows)
|
||||
- [Linux / MacOS](#linux--macos)
|
||||
- [Installing pytest within a virtual environment](#installing-pytest-within-a-virtual-environment)
|
||||
- [Running the tests](#running-the-tests)
|
||||
- [Failures](#failures)
|
||||
- [Extra arguments](#extra-arguments)
|
||||
- [Stop After First Failure [`-x`]](#stop-after-first-failure--x)
|
||||
- [Failed Tests First [`--ff`]](#failed-tests-first---ff)
|
||||
- [Recommended Workflow](#recommended-workflow)
|
||||
- [Using PDB, the Python Debugger, with pytest](#using-pdb-the-python-debugger-with-pytest)
|
||||
- [Extending your IDE](#extending-your-ide)
|
||||
- [Additional information](#additional-information)
|
||||
- [Adding pytest to your PATH](#adding-pytest-to-your-path)
|
||||
- [Windows](#windows-1)
|
||||
- [Fixing warnings](#fixing-warnings)
|
||||
|
||||
We recommend you install [pytest](http://pytest.org/en/latest/) with the following plugins:
|
||||
---
|
||||
|
||||
- [pytest-cache](http://pythonhosted.org/pytest-cache/)
|
||||
- [pytest-subtests](https://github.com/pytest-dev/pytest-subtests)
|
||||
- [pytest-pylint](https://github.com/carsongee/pytest-pylint)
|
||||
## Pytest
|
||||
|
||||
The PyTest [Getting Started Guide](https://docs.pytest.org/en/latest/getting-started.html) has quick general instructions, although they do not cover installing the plugins.
|
||||
Continue reading below for plugin installation.
|
||||
_Official pytest documentation can be found on the [pytest Wiki](https://pytest.org/en/latest/) page._
|
||||
|
||||
We also recommend [pylint](https://pylint.pycqa.org/en/latest/user_guide/), as it is part of our automated feedback on the website, and can be a very useful (if noisy!) code analysis tool.
|
||||
Pytest lets you test your solutions using our provided tests, and is what we use to validate your solutions on the website.
|
||||
|
||||
Pylint can be a bit much, so this [tutorial](https://pylint.pycqa.org/en/latest/tutorial.html) can be helpful for getting started, as can this overview of [Code Quality: Tools and Best Practices](https://realpython.com/python-code-quality/) from Real Python.
|
||||
### Installing pytest Globally
|
||||
|
||||
Finally, [this site](https://pycodequ.al/docs/pylint-messages.html) is a great place to look up more human-readable descriptions of Pylint linting messages.
|
||||
Pytest can be installed and updated using the built-in Python utility `pip`.
|
||||
|
||||
#### Windows
|
||||
|
||||
### Installing `pytest`
|
||||
```powershell
|
||||
PS C:\Users\foobar> python3 -m pip install pytest pytest-cache pytest-subtests pytest-pylint
|
||||
Successfully installed pytest-6.2.5 ...
|
||||
```
|
||||
|
||||
To install `pytest`, run the following command in the environment (_active `venv`, `conda env`, `docker container`, `vm` or other project space with Python 3.8 installed_):
|
||||
#### Linux / MacOS
|
||||
|
||||
```bash
|
||||
pip3 install pytest pytest-cache pytest-subtests pytest-pylint
|
||||
$ python3 -m pip install pytest pytest-cache pytest-subtests pytest-pylint
|
||||
Successfully installed pytest-6.2.5 ...
|
||||
|
||||
```
|
||||
|
||||
If you get a `command not found` response from your system, you can find a
|
||||
tutorial on how to install `pip`
|
||||
[here](https://pip.pypa.io/en/stable/installing/).
|
||||
|
||||
Once the installation has completed, you can check what the default version of `pytest` is by running the following:
|
||||
To check if the installation was succesful:
|
||||
|
||||
```bash
|
||||
pytest --version
|
||||
$ python3 -m pytest --version
|
||||
pytest 6.2.5
|
||||
```
|
||||
|
||||
## Testing
|
||||
If you do not want to precede every command with `python3 -m` please refer to [adding to PATH](#adding-to-path) at the end of this document.
|
||||
|
||||
### Run All Tests for an Exercise
|
||||
#### Installing pytest within a virtual environment
|
||||
|
||||
To run all tests for a specific exercise (_we will take the `bob.py` exercise as
|
||||
an example here_), navigate to the directory where that exercise has been
|
||||
downloaded and run the following in the terminal:
|
||||
*For more information about virtual environments please refer to the [TOOLS](./TOOLS.md) file.*
|
||||
|
||||
When installing pytest or any other module(s), make sure that you have [activated your environment](.\TOOLS.md#activating-your-virtual-environment). After which you can run:
|
||||
|
||||
```bash
|
||||
pytest bob_test.py
|
||||
$ pip install pytest pytest-cache pytest-subtests pytest-pylint
|
||||
Successfully installed pytest-6.2.5 ...
|
||||
```
|
||||
|
||||
**Note:** To run the tests you need to pass the name of the testsuite file to
|
||||
`pytest` (_generally, this will be the file ending with `_test.py`_), **NOT** the file that was
|
||||
created to solve the exercsim problem (which is the _solution implementation_).
|
||||
`PyTest` needs the test definitions in the `_test.py` file in order to know how to call, run, and exercise the _solution implementation_ code.
|
||||
Since there are no test cases defined in the _solution implementation_, `pytest` will just return a positive result, specifying that it found and ran zero tests. Like this:
|
||||
### Running the tests
|
||||
|
||||
|
||||
```
|
||||
============================= bob.py ==============================
|
||||
|
||||
---------------------------------------------------------------------
|
||||
|
||||
Ran 0 tests in 0.000s
|
||||
|
||||
OK
|
||||
```
|
||||
|
||||
|
||||
### More `pytest` Examples
|
||||
|
||||
Below are some additional examples and details for getting up and running quickly with Pytest.
|
||||
[How to invoke pytest](https://docs.pytest.org/en/latest/how-to/usage.html#usage) and [pytest command-line flags](https://docs.pytest.org/en/latest/reference/reference.html#command-line-flags) offer full details on all of pytests run options.
|
||||
|
||||
|
||||
#### Stop After First Failure
|
||||
The above will run all the tests, whether they fail or not. If you'd rather stop
|
||||
the process and exit on the first failure, run:
|
||||
To run the tests, go to the folder where the exercise is stored using `cd` in your terminal (_replace `{exercise-folder-location}` below with the path_).
|
||||
|
||||
```bash
|
||||
pytest -x bob_test.py
|
||||
$ cd {exercise-folder-location}
|
||||
```
|
||||
|
||||
#### Failed Tests First
|
||||
|
||||
`pytest-cache` remembers which tests failed, and can run those tests first.
|
||||
The file you'll want always ends with `_test.py`.
|
||||
This file contains the tests for your solution, and are the same tests which run on the website.
|
||||
Now run the following command in your terminal, replacing `{exercise_test.py}` with the location/name of the test file:
|
||||
|
||||
```bash
|
||||
pytest --ff bob_test.py
|
||||
$ python3 -m pytest {exercise_test.py}
|
||||
==================== 7 passed in 0.08s ====================
|
||||
```
|
||||
|
||||
#### Running All Tests for All Exercises
|
||||
#### Failures
|
||||
|
||||
When your code returns an incorrect or unexpected value, pytest returns all the failed tests and the returned and expected values of each. Look at the following failed test file:
|
||||
|
||||
```bash
|
||||
cd exercism/python/
|
||||
pytest
|
||||
$ python3 -m pytest {exercise_test.py}
|
||||
=================== FAILURES ====================
|
||||
______________ name_of_failed_test ______________
|
||||
# Test code inside of {exercise_test.py} that failed.
|
||||
...
|
||||
E TypeOfError: ReturnedValue != ExpectedValue
|
||||
|
||||
exercise_test.py:{line_of_failed_test}: TypeOfError
|
||||
============ short test summary info ============
|
||||
FAILED exercise_test.py::ExerciseTest::name_of_failed_test
|
||||
========== 1 failed, 2 passed in 0.13s ==========
|
||||
```
|
||||
|
||||
## Recommended Workflow
|
||||
### Extra arguments
|
||||
|
||||
Try this command while working on exercises:
|
||||
If you really want to be specific about what pytest returns on your screen, here are some handy arguments that allows you to configure its behavior.
|
||||
|
||||
#### Stop After First Failure [`-x`]
|
||||
|
||||
Running the `pytest -x {exercise_test.py}` command, will run the tests like normal, but will stop the tests after the first failed test. This will help when you want to debug a single failure at a time.
|
||||
|
||||
```bash
|
||||
cd exercism/python/bob
|
||||
pytest -x --ff bob_test.py
|
||||
$ python -m pytest -x example_test.py
|
||||
=================== FAILURES ====================
|
||||
_______________ example_test_foo ________________
|
||||
...
|
||||
...
|
||||
============ short test summary info ============
|
||||
FAILED example_test.py::ExampleTest::example_test_foo
|
||||
!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!
|
||||
========== 1 failed, 5 passed in 0.28s ==========
|
||||
```
|
||||
|
||||
## PDB
|
||||
#### Failed Tests First [`--ff`]
|
||||
|
||||
Typing pdb on the command line will drop you into the python debugger when a test fails.
|
||||
To learn how to usepdb, check out the
|
||||
[documentation](https://docs.python.org/3/library/pdb.html#debugger-commands).
|
||||
`pytest-cache` remembers which tests failed last time you ran `pytest`, running `pytest --ff {exercise_test.py}` will run those previously failed tests first, then it will continue with the rest of the tests. This might speed up your testing if you are making a lot of smaller fixes.
|
||||
|
||||
```bash
|
||||
pytest --pdb bob_test.py
|
||||
$ python -m pytest --ff bob_test.py
|
||||
==================== 7 passed in 503s ====================
|
||||
```
|
||||
|
||||
#### Recommended Workflow
|
||||
|
||||
We recommend using the following commands to make your debugging easier and (possibly) faster:
|
||||
|
||||
First change your working directory to the directory of the exercise you want to test:
|
||||
|
||||
```bash
|
||||
cd path/to/exercise
|
||||
```
|
||||
|
||||
Then, run the tests together with the previously explained arguments `-x` and`--ff`:
|
||||
|
||||
```bash
|
||||
pytest -x -ff bob_test.py
|
||||
```
|
||||
|
||||
This will test your solution. When `pytest` encounters a failed test, the program will stop and tell you which test failed. When you run the test again, `pytest` will first test that failed test, then continue with the rest.
|
||||
|
||||
#### Using PDB, the Python Debugger, with pytest
|
||||
|
||||
If you want to truly debug like a pro, use the `--pdb` argument after the `pytest` command.
|
||||
|
||||
```bash
|
||||
$ python3 -m pytest --pdb bob_test.py
|
||||
=============== 4 passed in 0.15s ===============
|
||||
```
|
||||
|
||||
When a test fails, `PDB` allows you to look at variables and how your code responds. If you want to learn how to use the `PDB` module, have a look at the [Python Docs](https://docs.python.org/3/library/pdb.html#module-pdb) or [this](https://realpython.com/python-debugging-pdb/) Real Python article.
|
||||
|
||||
## Extending your IDE
|
||||
|
||||
If you'd like to extend your IDE with some tools that will help you with testing and improving your code, check the [TOOLS](./TOOLS.md) page. We go into multiple IDEs, editors and some useful extensions.
|
||||
|
||||
## Additional information
|
||||
|
||||
### Adding pytest to your PATH
|
||||
|
||||
**Note:** If you are running a [virtual environment](.\TOOLS.md) you do not need to *add to path* as it should work fine.
|
||||
|
||||
Typing `python3 -m` every time you want to run a module can get a little annoying. You can add the `Scripts` folder of your Python installation to your path. If you do not know where you have installed Python, run the following command in your terminal:
|
||||
|
||||
```bash
|
||||
$ python3 -c "import os, sys; print(os.path.dirname(sys.executable))"
|
||||
{python_directory}
|
||||
```
|
||||
|
||||
The *returned* directory is where your Python version is installed, in this tutorial it is referred to as `{python_directory}`.
|
||||
|
||||
#### Windows
|
||||
|
||||
Click the `Windows Start` button and lookup *Edit the system environment variables* and press enter. Next press, `Environment Variables...`:
|
||||
|
||||

|
||||
|
||||
Then find the `Path` variable in your *User variables*, select it, and click `Edit...`:
|
||||
|
||||

|
||||
|
||||
Then add a new line, as shown in the picture, replacing `{python_directory}` with your Python installation's directory:
|
||||
|
||||

|
||||
|
||||
### Fixing warnings
|
||||
|
||||
It is possible that you will get `warnings` about "unknown markers" when running a test that uses our _new_ syntax.
|
||||
|
||||
To resolve this issue, we use a `pytest.ini` file, which can be downloaded from the top level of the Python track directory: [pytest.ini](https://github.com/exercism/python/blob/main/pytest.ini).
|
||||
|
||||
You can also create your own file with the following content:
|
||||
|
||||
```ini
|
||||
[pytest]
|
||||
markers =
|
||||
task: A concept exercise task.
|
||||
```
|
||||
|
||||
Whenever you run your tests, make sure that this file is in your _root_ or _working_ directory.
|
||||
|
||||
_More information on customizing pytest can be found in the [PyTest docs](https://docs.pytest.org/en/6.2.x/customize.html#pytest-ini)_
|
||||
221
docs/TOOLS.md
@@ -1,44 +1,207 @@
|
||||
# Tools
|
||||
|
||||
## Visual Studio on Windows
|
||||
A list of tools, IDEs and editors that can help you write and debug your _Python_ code.
|
||||
|
||||
Follow the installation instructions for [Python Tools for Visual Studio](https://pytools.codeplex.com/wikipage?title=PTVS%20Installation)
|
||||
*Disclaimer: This is a collection of tools that are popular in our community. We do not have any financial affiliation with any of the tools listed below. We think these tools do their job and there are most certainly other tools that can do those jobs as well.*
|
||||
|
||||
You can either start by creating your own project for working with the Exercism problems or you can download a Visual Studio solution that is already set up.
|
||||
Before you can start coding, make sure that you have the proper version of Python installed. Exercism currently supports `Python 3.8` and above. For more information, please refer to [Installing Python locally](https://exercism.org/docs/tracks/Python/installation).
|
||||
|
||||
### Exercism.io Visual Studio Template
|
||||
---
|
||||
|
||||
This is a Visual Studio template that comes pre-configured to work on the problems in as many languages as Visual Studio supports.
|
||||
- [Environments](#environments)
|
||||
- [Virtualenv](#venv)
|
||||
- [Conda](#conda)
|
||||
|
||||

|
||||
- [Tools](#tools)
|
||||
- [Environments](#environments)
|
||||
- [Venv](#venv)
|
||||
- [Creating your virtual environment](#creating-your-virtual-environment)
|
||||
- [Activating your virtual environment](#activating-your-virtual-environment)
|
||||
- [Virtual Environment wrapper](#virtual-environment-wrapper)
|
||||
- [Conda](#conda)
|
||||
- [Activating your conda environment](#activating-your-conda-environment)
|
||||
- [Editors and IDEs](#editors-and-ides)
|
||||
- [Visual Studio Code](#visual-studio-code)
|
||||
- [Python for VS Code](#python-for-vs-code)
|
||||
- [Selecting the interpreter](#selecting-the-interpreter)
|
||||
- [Other features](#other-features)
|
||||
- [PyCharm](#pycharm)
|
||||
- [Selecting the interpreter](#selecting-the-interpreter-1)
|
||||
- [Other features](#other-features-1)
|
||||
- [Spyder IDE](#spyder-ide)
|
||||
- [Selecting the interpreter](#selecting-the-interpreter-2)
|
||||
- [Other features](#other-features-2)
|
||||
- [Sublime text](#sublime-text)
|
||||
- [Notable extensions](#notable-extensions)
|
||||
- [JupyterLab](#jupyterlab)
|
||||
- [Notable extensions](#notable-extensions-1)
|
||||
|
||||
1. Download the [Exercism.io Visual Studio Template](https://github.com/rprouse/Exercism.VisualStudio) from GitHub by clicking the Download Zip button on the page.
|
||||
2. Unzip the template into your exercises directory, for example `C:\src\exercises`
|
||||
2. Install the [Exercism CLI](http://exercism.io/cli)
|
||||
3. Open a command prompt to your exercise directory
|
||||
4. Add your API key to exercism `exercism configure --key=YOUR_API_KEY`
|
||||
5. Configure your source directory in exercism `exercism configure --dir=C:\src\exercises`
|
||||
6. [Fetch your first exercise](http://exercism.io/languages/python) `exercism fetch python hello-world`
|
||||
7. Open the Exercism solution in Visual Studio
|
||||
8. Expand the Exercism.python project
|
||||
9. Click on **Show All Files** in Solution Explorer (See below)
|
||||
10. The exercise you just fetched will appear greyed out. Right click on the folder and **Include In Project**
|
||||
11. Get coding...
|
||||
---
|
||||
|
||||

|
||||
## Environments
|
||||
|
||||
To run the tests, you can do so at the command line, or within Visual Studio.
|
||||
Python environments are like separate Python installations: they can organize your workflow and projects by keeping the packages you install inside that project. This helps avoid bugs generated by something that you imported in another project.
|
||||
|
||||

|
||||
There are two major *virtual environments* in use today, `virtualenv` and `conda`. Both of are easy to install.
|
||||
|
||||
## Code Style and Linting
|
||||
### Venv
|
||||
|
||||
There's a style guide called [PEP8](http://legacy.python.org/dev/peps/pep-0008/) that many Python projects adhere to.
|
||||
Read it when you get a chance!
|
||||
Also known as `virtualenv`, *venv* is a light-weight solution to virtual environments. It creates a separate Python binary for every virtual environment and stores that inside your directory.
|
||||
|
||||
If you just want a quick overview of some problems in your code, use [pylint](http://www.pylint.org/)!
|
||||
It can be pretty picky though, so take its results with a grain of salt.
|
||||
If you don't agree with one of its points, that's a good topic for a discussion in the comments for your program!
|
||||
#### Creating your virtual environment
|
||||
|
||||
If you'd rather have a tool take care of your style issues, take a look at [autopep8](https://github.com/hhatto/autopep8)!
|
||||
Run `autopep8 -d mycode.py` to get a diff of the changes it proposes and `autopep8 -i mycode.py` to format the code inplace!
|
||||
To create a virtual environment, `cd` to the directory you want to store your environments in. Next, run the `venv` command with the name of a folder where you want to store this particular **environment**. Common convention is to call that folder `<project-name-here>_venv`:
|
||||
|
||||
```bash
|
||||
$ python3 -m venv {name_of_virtualenv}
|
||||
created virtual environment ... in 8568ms
|
||||
|
||||
#### Activating your virtual environment
|
||||
|
||||
To activate the virtual environment on Windows, run the following command:
|
||||
|
||||
```powershell
|
||||
PS> .\{name_of_virtual_env}\Scripts\activate
|
||||
(venv) PS> _
|
||||
```
|
||||
|
||||
To activate your virtual environment on Linux, or MacOS X, run the following command:
|
||||
|
||||
```bash
|
||||
$ source {name_of_virtual_env}/bin/activate
|
||||
(venv) $ _
|
||||
```
|
||||
|
||||
From this terminal you can now run `pip` commands. All of the packages installed in the environment will be installed inside `{name_of_virtual_env}/Lib`.
|
||||
|
||||
#### Virtual Environment wrapper
|
||||
|
||||
Reference: [Wiki](https://virtualenvwrapper.readthedocs.io/en/latest/)
|
||||
|
||||
The `virtualenvwrapper` module manages all your virtual environments in one place. You can create, copy, delete and switch between environments using the tools the module provides. It also allows you to add additional tools using extensions. You can create your own extension using the tutorial found [here](https://virtualenvwrapper.readthedocs.io/en/latest/plugins.html#plugins).
|
||||
|
||||
### Conda
|
||||
|
||||
*Latest download can be found [here](https://www.anaconda.com/products/individual)*
|
||||
|
||||
Packaged with *Anaconda*, `conda` environments are similar to [venv environments](#venv) with the key difference being that `conda` can support the `R programming language`. Anaconda is most commonly used by researchers and data scientists because it contains a set of tools and programs tailored to them. The [Spyder IDE](#spider-ide) is one of the tools that comes with Anaconda.
|
||||
|
||||
To create a new `conda` environment, go to the *Anaconda Navigator*. Click on `environments` and then press the `Create` button. Fill in a name for your `conda` environment, select Python `3.8 or above` and click `Create`:
|
||||
|
||||

|
||||
|
||||
#### Activating your conda environment
|
||||
|
||||
Activating your `conda` environment is easy, just click the `►` button next to your *environment*, then press `Open Terminal`. This should open a new terminal with an interface for your `conda` environment.
|
||||
|
||||
From here you can run `conda` commands to install python packages from the Anaconda repository, or run regular `pip` commands to install python packages from PyPi inside your environment. All libraries will automatically be installed inside the `conda` environment.
|
||||
|
||||
---
|
||||
|
||||
## Editors and IDEs
|
||||
|
||||
### Visual Studio Code
|
||||
|
||||
Visual studio code (VS Code) is a code editor created by Microsoft. It is a general-purpose editor and not designed specifically for any one language. You can extend the editor using extensions; it comes with some great extensions already installed.
|
||||
|
||||
#### Python for VS Code
|
||||
|
||||
Extension: _Extension-id: ms-python.python_
|
||||
|
||||

|
||||
|
||||
The Python extension from Microsoft is extremely useful because of its range of features. Notably it supports testing and has a testing explorer! It has many other features that you can view on [its homepage](https://marketplace.visualstudio.com/items?itemName=ms-python.python).
|
||||
|
||||
##### Selecting the interpreter
|
||||
|
||||
The Python extensions supports the switching between multiple `interpreters`. This allows you to use different Python environments for different projects. This is also useful for when you are using `venv` or `conda`, which you can find more about in the [environments section](#environments).
|
||||
|
||||
Click on the "Select interpreter" button in the lower left-hand of your window, another window should pop up where you can select the interpreter you want to use.
|
||||
|
||||

|
||||
|
||||
##### Other features
|
||||
|
||||
The Python plugin also comes with some other features that can help you debug and improve your Python code, here are some of those tools.
|
||||
|
||||
[Test discovery tool](https://code.visualstudio.com/docs/python/testing#_configure-tests) - A tool that generates a tree containing all the *pytests* and *unittests* inside a directory. It will give you an easier and more readable interface for *pytest*.
|
||||
|
||||
[Debugging tool](https://code.visualstudio.com/docs/python/testing#_configure-tests) - This tool will help you debug your code. You can set a `breakpoint` on a line. The debugging tool then allows you to view all *private* and *global* variables at that point in your program.
|
||||
|
||||
[Linting](https://code.visualstudio.com/docs/python/testing#_configure-tests) - Linting looks out for simple mistakes in your code and notifies you when you write code which does not follow best practices according to PEP. Exercism currently focuses on following [PEP 8](https://www.python.org/dev/peps/pep-0008/).
|
||||
|
||||
[Formatting](https://code.visualstudio.com/docs/python/editing#_formatting) - This tool automatically formats your code to look a certain way. We recommend `autopep8` because it adheres to [PEP 8](https://www.python.org/dev/peps/pep-0008/). Other formatters are also supported.
|
||||
|
||||
[Config reference](https://code.visualstudio.com/docs/python/settings-reference) - If you want to configure your Python development on VS Code to behave exactly like you want it, view this reference guide. It explains options for the extensions which can really improve your coding experience.
|
||||
|
||||
---
|
||||
|
||||
### PyCharm
|
||||
|
||||
PyCharm is an *Integrated Development Environment* built by JetBrains. It is specialized to work for Python and is commonly used among professionals. You can extend it with various paid and unpaid plugins, but out-of-the-box it comes with a load of features already available.
|
||||
|
||||
#### Selecting the interpreter
|
||||
|
||||
Open your project, then navigate to `File` >> `Settings` >> `Project: ...` >> `Python Interpreter`. Click on the dropdown menu and select the environment you will be using. If the environment you would like to use is not in the list click on the `Show All...` button:
|
||||
|
||||

|
||||
|
||||
From there click on the `+` button to add a new interpreter. Select the type of interpreter on the left. We suggest you either run a [conda]() or [virtualenv]() environment, but running the *system interpreter* works fine, too. Once you selected your interpreter, press the `Okay` button.
|
||||
|
||||

|
||||
|
||||
#### Other features
|
||||
|
||||
Below are some other features that we won't cover in this guide, but are really useful for development.
|
||||
|
||||
[Running Tests](https://www.jetbrains.com/help/pycharm/pytest.html#run-pytest-test) - Running tests directly from a GUI in your window is really easy, but don't forget to look at the [pytest parameters](TESTS.md#extra-arguments) to set it up to your liking.
|
||||
|
||||
[Debug Tools](https://www.jetbrains.com/help/pycharm/debugging-code.html) - Debugging in PyCharm is a great way to take advantage of its tools.
|
||||
|
||||
---
|
||||
|
||||
### Spyder IDE
|
||||
|
||||
Spyder is a Python IDE tailored for the scientific community. It packs some really good debugging utilities and it has an integrated *IPython terminal*. Out-of-the-box it does not come with support for *unittests* and *pytests*, but there is an official plugin available.
|
||||
|
||||
#### Selecting the interpreter
|
||||
|
||||
To change the interpreter, go to `tools` >> `Preferences` >> `Python interpreter`. You can either select the global interpreter defined by Spyder or you can enter the path to your own Python environment.
|
||||
|
||||

|
||||
|
||||
Do not forget to click `Apply` once you selected your interpreter.
|
||||
|
||||
#### Other features
|
||||
|
||||
[Spyder Unittest](https://github.com/spyder-ide/spyder-unittest/releases/latest) - This plugin gives you a built-in interface for running your tests. This link will bring you to the latest release on GitHub.
|
||||
|
||||
[Code analyzer](https://docs.spyder-ide.org/current/panes/pylint.html#using-the-code-analyzer) - This tool can analyze your code, detecting bad practices, and catching bugs before you run your program. You can also configure it to only show [PEP 8](https://www.python.org/dev/peps/pep-0008/) violations.
|
||||
|
||||
[Debugging](https://docs.spyder-ide.org/current/panes/debugging.html) - This tool will help you debug your code, it allows you to set a `breakpoint` on a line. The debugging tool will then let you go through your Python code and quickly notice where it goes wrong.
|
||||
|
||||
[Variable Explorer](https://docs.spyder-ide.org/current/panes/variableexplorer.html) - This tool is really useful in combination with the debugger. It allows you to view all the active variables in your code and it even allows you to edit of the values inside those variables.
|
||||
|
||||
---
|
||||
|
||||
### Sublime text
|
||||
|
||||
Sublime text is text editor for coding, made by *Sublime HQ Pty Ltd*. It is similar to [VS Code](#visual-studio-code) in many regards. Sublime text is very customizable. It comes with an auto-complete engine and with Python syntax highlighting out-of-the-box.
|
||||
|
||||
#### Notable extensions
|
||||
|
||||
Sublime Text comes with a lot of tools already, but some of these tools could make your Python development easier. Make sure you have *package control* installed, go to `Tools` >> `Install Package Control...`. To install packages open your command pallet and type in `Package Control: Install Package`, here you can enter the name of the packages you'd like to install.
|
||||
|
||||
[Terminus](https://packagecontrol.io/packages/Terminus) - A terminal for Sublime. Terminus is fully featured and has support for Windows operating systems. This package is a must-have for running `pytest` scripts.
|
||||
|
||||
[SublimeLinter](https://packagecontrol.io/packages/SublimeLinter) - A linter for sublime. This tool notifies you about Python code that does not follow best practices. We recommend you set it up to use PEP-8.
|
||||
|
||||
### JupyterLab
|
||||
|
||||
[Jupyter lab](https://jupyter.org/install) is an in-browser code editor that runs on your machine, combining the ability to write Markdown along side executable code and data. Jupyter supports multiple programming languages via `kernels` and can display graphs and tables as well as code and text.
|
||||
|
||||
Jupyter comes pre-packaged with Anaconda. You can also install it in a **virtual environment** using `pip` or `conda`. Jupyter will use that environment and its Python version. Jupyter is most well-known for its code "notebooks". It is commonly used among data-scientists.
|
||||
|
||||
#### Notable extensions
|
||||
|
||||
The built-in tools for Jupyter Lab are sufficient for almost all exercises on Exercism. Most extensions to Jupyter focus more on data-science applications and not general-purpose programming. Explore extensions at your own pleasure.
|
||||
|
||||
|
Before Width: | Height: | Size: 22 KiB |
BIN
docs/img/Anaconda-Conda-New.png
Normal file
|
After Width: | Height: | Size: 31 KiB |
BIN
docs/img/PyCharm-Config-InterpreterDropDown.png
Normal file
|
After Width: | Height: | Size: 18 KiB |
BIN
docs/img/PyCharm-Config-InterpreterNew.png
Normal file
|
After Width: | Height: | Size: 35 KiB |
|
Before Width: | Height: | Size: 8.4 KiB |
BIN
docs/img/Spyder-Config-Interpreter.png
Normal file
|
After Width: | Height: | Size: 32 KiB |
|
Before Width: | Height: | Size: 25 KiB |
BIN
docs/img/VSCode-EXT-Python-Header.png
Normal file
|
After Width: | Height: | Size: 33 KiB |
BIN
docs/img/VSCode-EXT-Python-SelectInterpreter-2.png
Normal file
|
After Width: | Height: | Size: 33 KiB |
BIN
docs/img/Windows-AddPythonPath.png
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
docs/img/Windows-EnvironmentVariables.png
Normal file
|
After Width: | Height: | Size: 7.7 KiB |
BIN
docs/img/Windows-SystemProperties.png
Normal file
|
After Width: | Height: | Size: 25 KiB |
@@ -1,27 +1,19 @@
|
||||
# Tests
|
||||
|
||||
You can run the included tests by typing `pytest <exercisename>_test.py` on the command line from within the exercise's directory.
|
||||
To run the included *tests*, run the test file using the `pytest` module, replacing `{exercise_name}`:
|
||||
|
||||
You can also tell Python to run the pytest module on the command line from either within the exercise directory or with a path to the exercise directory.
|
||||
`python -m pytest <exercisename>_test.py` from within the exercise directory.
|
||||
```bash
|
||||
$ python3 -m pytest {exercise_name}_test.py
|
||||
```
|
||||
|
||||
`python -m pytest /fully/qualified/path/to/<exercisename>/` OR `python -m pytest realtive/path/to/<exercisename>` from a non-exercise directory.
|
||||
Many IDE's and code editors have built-in support for using Pytest to run tests; check them out [here](https://github.com/exercism/python/blob/main/docs/TOOLS.md#editors-and-ides).
|
||||
|
||||
Many IDE's and code editors also have built-in support for using PyTest to run tests.
|
||||
For more information about running tests using `pytest`, checkout our [Python testing guide](https://github.com/exercism/python/blob/main/docs/TESTS.md#pytest).
|
||||
|
||||
- [Visual Studio Code](https://code.visualstudio.com/docs/python/testing)
|
||||
- [PyCharm Professional & Community Editions](https://www.jetbrains.com/help/pycharm/pytest.html#create-pytest-test)
|
||||
- [Atom](https://atom.io/packages/atom-python-test)
|
||||
- [Spyder](https://www.spyder-ide.org/blog/introducing-unittest-plugin/)
|
||||
- [Sublime](https://github.com/kaste/PyTest)
|
||||
- [vim-test](https://github.com/vim-test/vim-test)
|
||||
|
||||
See the [Python tests page](https://github.com/exercism/python/blob/main/docs/TESTS.md) for more information.
|
||||
|
||||
### Common `pytest` options
|
||||
### Common pytest options
|
||||
|
||||
- `-v` : enable verbose output.
|
||||
- `-x` : stop running tests on first failure.
|
||||
- `--ff` : run failures from previous test before running other test cases.
|
||||
|
||||
For other options, see `python -m pytest -h`. PyTest documentation can be found [here](https://docs.pytest.org/en/latest/getting-started.html).
|
||||
For other options, see `python3 -m pytest -h`.
|
||||
|
||||