Python从同一级别的目录导入模块

时间:2017-11-07 21:41:36

标签: python unit-testing

我有以下解决方案结构:

  


  主/ SRC / myApi.py
  main / unittests / myApiTests.py

myApiTests.py中的

我尝试从Database导入课程myApi.py 但每次尝试我都会收到以下错误:

  

追踪(最近一次通话):     文件“S:... \ main \ UnitTests \ myApiTests.py”,第3行,in       来自myApi导入数据库   ModuleNotFoundError:没有名为'myApi'的模块

import sys
sys.path.append("../src")
from myApi import Database
import unittest


class BotApiTests(unittest.TestCase):
    def test_GetUserBalance_WhenUserDoesNotExist_ThenReturn0(self):
        testDatabase = Database('testDb.db')
        userName = "testUserName"

        result = testDatabase.GetUserBalance(userName)
        self.assertEqual(result, 0)

unittest.main()

我也试过

sys.path.append("..src")

sys.path.append("..")

from myApi import Database

但没有任何作用: - (

编辑:我在每个文件夹中都有一个空__init__.py

Edit2:完整代码:

主/ SRC / myApi.py:

import re
import string
import urllib.request
import sqlite3
import praw

class Database:   
    def __init__(self, name='cryptotipbot.db'):
        print("in __init__ -> ", (name))
        self.connection = sqlite3.connect(name, check_same_thread=False)
        self.database = self.connection.cursor()
        self.CreateDatabase()
        self.addressIndex = len(self.database.execute("SELECT * FROM usedAdresses").fetchall())

    def CreateDatabase(self):
        print("in CreateDatabase")
        self.database.execute("CREATE TABLE IF NOT EXISTS Users (redditUsername TEXT PRIMARY KEY, balance INTEGER)")
        self.connection.commit()
        self.database.execute("CREATE TABLE IF NOT EXISTS CommentsRepliedTo (commentId TEXT PRIMARY KEY)")
        self.connection.commit()
        self.database.execute("CREATE TABLE IF NOT EXISTS UsedAdresses (adressIndex INTEGER PRIMARY KEY, adress TEXT)")
        self.connection.commit()
        self.database.execute("CREATE TABLE IF NOT EXISTS DepositRequests (messageId TEXT PRIMARY KEY, adress TEXT, amount INTEGER)")
        self.connection.commit()

    def CreateUser(self, redditUsername):
        user = self.database.execute("SELECT * FROM Users WHERE redditUsername = ?", (redditUsername,)).fetchone()
        if not user:
            self.database.execute("INSERT INTO Users (redditUsername, balance) VALUES (?, ?)", (redditUsername, 0))
            self.connection.commit()

    def GetUserBalance(self, redditUsername):
        entry = self.database.execute("SELECT * FROM Users WHERE redditUsername = ?",(redditUsername,)).fetchone()
        if entry:
            balance = entry[1]
            return balance
        else:
            self.CreateUser(redditUsername)
            return self.GetUserBalance(redditUsername)

主/单元测试/ myApiTests.py

import unittest
from ..src import myApi

class BotApiTests(unittest.TestCase):
    def test_GetUserBalance_WhenUserDoesNotExist_ThenReturn0(self):
        testDatabase = Database('testDb.db')
        userName = "testUserName"

        result = testDatabase.GetUserBalance(userName)
        self.assertEqual(result, 0)


unittest.main()

0 个答案:

没有答案