3

Here is a scenario, i hope you can help me with.

I have python unit tests in /home/k/PR-TEST/Pr/test

kahmed@qatools:~/PR-TEST/Pr-test$ ls
_apache.py        archive  clienttest  decorators.py   fakeTecoreClient.py  fixtures.pyc     psqlharness.py  servertest   testhelp.pyc  testutils.pyc
apache_server.py  cache    commontest  decorators.pyc  fixtures.py          mysqlharness.py  runtests.sh     testhelp.py  testutils.py

and have the package that i am actually running tests against @ /home/k/PR/Pr named (directory named) pref

kahmed@qatools:~/PR/Pr$ pwd
/home/kahmed/PR/Pr
kahmed@qatools:~/PR/Pr$ ls
bin  conf  images  init.d  iped  logrotate.d  NEWS  pref  rpa  sbin  scripts  src  test  VERSION

Now, i cd to ~/PR-TEST/Pr-test, and run:

nosetests -s --with-coverage --cover-package /home/k/PR/Pr/pref  `find . -name "*test.py"`

but i get erros that it cannot find the packages in pref

Any idea how to resolve this ?

I have to make sure that the python environment knows about the package pr

4 Answers 4

4

If you cannot (or do not want to) modify PYTHONPATH before you start the program, you can modify sys.path during runtime to achive similar functionality.
But in this case you will need to wrap the call to nosetests script. Also please check that you do not have case-sensitivity issue with your package being called Pr vs pr.

Sign up to request clarification or add additional context in comments.

2 Comments

Any example ? of adding a custom variable PR_PATH during runtime ?
Just use the import os; os.getenv("PR_PATH") (docs.python.org/library/os.html#os.getenv) to get the value, and than add it to sys.path
1

You should add paths to your packages to PYTHONPATH environment variable

1 Comment

i want to use a custom Env Varable like PR_HOME, or PR_PATH
1

[For Windows]

If your packages are located in c:\temp\py\pkgs

import sys
sys.path.append('c:\\temp\\py\\pkgs')

1 Comment

This works on UNIX too, but with UNIX paths rather than windows `DRIVE:\foo\bar` paths.
0

You should put your application package in a directory "foo/app" and the tests in another sub directory called "foo/tests". Then run your tests from the foo directory so that they can import the app directly.

3 Comments

i cannot dictate that app and tests will be under foo
You could have a script that first sets its cwd to foo using os.chdir('foo') and then runs the app and/or tests but it’s a little clunky. I do it all the time but I’m not sure if it’s good practice…
On second thoughts, I would use import os; from pathlib import Path; os.chdir(Path(__file__).parents[0]); # run your code here so that it works regardless of where the starter script is run from, as long as your starter is in foo/.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.