PyCharm - run appropriate unit test every time a file is saved
I just switched to using PyCharm as my main Python editor and am curious to know how I can customize it so that every time I save a file, a single test corresponding to the modified file will be executed? (after poking around the documentation, I didn't find a clear explanation on how to do this)
source to share
This is how I decided to accomplish my task.
I am using file watchers ( PyCharm->Preferences->File Watchers
) to keep track of python source files for modifications. The source files are located in a directory named src
. For my purposes, each unit test is in a directory that is parallel src
to the named directory test_src
, in which each file src/[file_name].py
will have an associated test file named test_src/test_[file_name].py
. Unit tests will run with py.test
.
To do this, I use the following setting in the File Observers dialog:
File type: "Python Files"
Scope: "Only watch files in src directory"
Program: /usr/local/bin/py.test
Arguments: $ProjectFileDir$/test_src/test_$FileName$
Working directory: $ProjectFileDir$
I also unchecked "Sync files immediately" as I only want to run unit tests when the file is saved.
Note that Scope is used to specify which files will be tracked.
source to share