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)

+3


source to share


3 answers


You can try File Watchers for a file modification task.



Configuration example: Config

+3


source


When you run unittest, you get a new unittest window. There is a button in this window where you can turn autotesting on and off, see below. You can also set the autotest delay (look at the gear icon in the same window.



auto.test

+4


source


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.

+1


source







All Articles