2

I have a folder structure:

app/
|
|-src/
| |
| |-Code.py
|
|-tests/
  |
  |-__init__.py
  |-test_Code.py

I run my tests by issuing a command:

app$ python3 -m unittest discover

And as a result all my tests from test_Code.py file are run.

I would like to run a single test from test_Code.py file.

The contents of 'test_Code.py' are:

import unittest
from src.Code import Code

class TestCase1(unittest.TestCase):

    def test_Code1(self):
        ...

    def test_Code2(self):
        ...

if __name__ == '__main__':
    unittest.main()

There is a question about running a single test: Running single test from unittest.TestCase via command line.

The answer given is:

python testMyCase.py MyCase.testItIsHot

which in my case would be (I think):

app$ python3 tests/test_Code.py TestCase1.test_Code1

Unfortunately this results in an error:

ImportError: No module named 'src'

There has been a comment that the proposed approach does not work, should the tests be in a subdirectory. A resolution is proposed:

python test/testMyCase.py test.MyCase.testItIsHot

I therefore try:

app$ python3 tests/test_Code.py test_Code.TestCase1.test_Code1

Which results in the same error.

I have also tried, among a couple of different things:

app$ python3 -m unittest tests/test_Code.py test_Code.TestCase1.test_Code1

But this runs all the tests.

The question is then: how to run a single test via a command line when tests are located within a sibling folder?

Community
  • 1
  • 1
  • At what level are you running the tests from? At app level? Is "app" a python module or a simple module? – apatniv Feb 27 '17 at 00:16
  • @VivekAkupatni They are run at `app` level. What do you mean by module or simple module? There is no `__init__.py` in `app` folder, if this is what you are asking for. –  Feb 27 '17 at 00:26
  • Sorry I meant directory or python module – apatniv Feb 27 '17 at 10:54

1 Answers1

4

This should work.

python -m unittest -v tests.test_Code.TestCase1.test_Code1

Assuming that src is a also a module

apatniv
  • 1,771
  • 10
  • 13