Forked from tasdikrahman/python_tests_dir_structure.md
Created
October 25, 2017 02:34
-
-
Save wangbinyq/4c7fe0763bef3f20efb2d7c7de9b8e4e to your computer and use it in GitHub Desktop.
Revisions
-
prodicus renamed this gist
Jan 25, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,7 @@ ## A Typical directory structure for running tests using `unittest` Ref : [stackoverflow](http://stackoverflow.com/a/24266885) The best solution in my opinion is to use the `unittest` [command line interface][1] which will add the directory to the ``sys.path`` so you don't have to (done in the `TestLoader` class). For example for a directory structure like this: -
prodicus created this gist
Jan 25, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,64 @@ ## A Typical directory structure for running tests using `unittest` The best solution in my opinion is to use the `unittest` [command line interface][1] which will add the directory to the ``sys.path`` so you don't have to (done in the `TestLoader` class). For example for a directory structure like this: new_project ├── antigravity.py └── test_antigravity.py You can just run: $ cd new_project $ python -m unittest test_antigravity For a directory structure like yours: new_project ├── antigravity │ ├── __init__.py # make it a package │ └── antigravity.py └── test ├── __init__.py # also make test a package └── test_antigravity.py And in the test modules inside the `test` package, you can import the `antigravity` package and its modules as usual: # import the package import antigravity # import the antigravity module from antigravity import antigravity # or an object inside the antigravity module from antigravity.antigravity import my_object **Running a single test module:** To run a single test module, in this case `test_antigravity.py`: $ cd new_project $ python -m unittest test.test_antigravity Just reference the test module the same way you import it. **Running a single test case or test method:** Also you can run a single `TestCase` or a single test method: $ python -m unittest test.test_antigravity.GravityTestCase $ python -m unittest test.test_antigravity.GravityTestCase.test_method **Running all tests:** You can also use [test discovery][2] which will discover and run all the tests for you, they must be modules or packages named `test*.py` (can be changed with the `-p, --pattern` flag): $ cd new_project $ python -m unittest discover This will run all the `test*.py` modules inside the `test` package. [1]: https://docs.python.org/2/library/unittest.html#command-line-interface [2]: https://docs.python.org/2/library/unittest.html#test-discovery