简体   繁体   中英

Python Travis-CI: Importing main code in test code

I am writing a simple package in python. In the root directory, my structure currently is,

  • my_package
    • init .py
    • main_code.py
  • tests
    • init .py
    • test_main_code.py

I want to import all functions from main_code.py in test_main_code.py. To achieve this, I did

import sys
import os
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + '\..' + '\my_package')
from main_code import *

This runs smoothly on my system. But when I push the code to github and tests are run on travis-ci, all tests fail and return NameError. It says given function name not defined. How do I make sure that tests run on travis-ci as well ?

Paths could be different if you run the test somewhere else, so I wouldn't trust any import related to a particular location. In any case, I always try to run the tests from the root of the project .

Then, you can simply try:

from my_package.main_code import *

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM