質問編集履歴

2

質問内容書き換え他。

2016/03/06 08:03

投稿

manzyun
manzyun

スコア2244

test CHANGED
@@ -1 +1 @@
1
- nose2でしか動かないテストコードpudbなどステップ実行したい。
1
+ 自作モジュールのテストコードがImportError動きません
test CHANGED
@@ -6,6 +6,8 @@
6
6
 
7
7
  - Python3.5
8
8
 
9
+ - Flask
10
+
9
11
  - MongoDB
10
12
 
11
13
 
@@ -28,23 +30,329 @@
28
30
 
29
31
 
30
32
 
31
- ここら質問なのですが現在このコードテストコードを`nose2`で動かして`pudb`でデバッグという制作方法をしているのでが、そもそもテストコードがまもに動かないため、テストコード自体をステップ実行したいと思っております。
32
-
33
-
34
-
35
- すが`nose2`をアプリケーションコードのディレクリで実行場合はくのですが、
36
-
37
- `pudb`では`ImportError: No module named 'setsuna'`
38
-
39
- `python -m unittest`では`ImportError: Failed to import test module: setsuna.test.test_models``ImportError: No module named 'conf'`
40
-
41
-
42
-
43
- 返ってきます。
44
-
45
-
46
-
47
- importまわりの問題だと思うのですが、個人的には「`sys.path`にディレクトリを追加して片付けるのはクールじゃない」(こういう考えがバカバカしいのかもしれませんが)と思っているのですが、ご指摘あればご教授いただけると幸いです。
33
+ 、この構成でリポジトリのコードだと、unittestやnose2動かすと
34
+
35
+ `ImportError: No module named 'conf'`
36
+
37
+ てしまいテス実行されません。ま、pudbでテストコードをかしても
38
+
39
+ `ImportError: No module named 'setsuna'`
40
+
41
+ なってしまい、テストが全く実行出来ずに困っています。
42
+
43
+
44
+
45
+ Pythonのモジュールインポートの話だと思うのです
46
+
47
+ もしかしたらconf.pyの中身が変数しか無いことが原因なのかもしれないのかと思っているのですが、僕自身現状がどうなっているのかわかりません。
48
+
49
+
50
+
51
+ 参考までに、該当するテストコードとテスト対象のコードを貼り付けます。
52
+
53
+
54
+
55
+ ```python
56
+
57
+ import unittest
58
+
59
+
60
+
61
+ from setsuna import conf, models
62
+
63
+ from unittest import TestCase, expectedFailure
64
+
65
+ from pymongo import MongoClient
66
+
67
+ import json
68
+
69
+ import datetime
70
+
71
+ import calendar
72
+
73
+
74
+
75
+
76
+
77
+ testdata = {"content": "美味しい美味しいスープカレー",
78
+
79
+ "limit": calendar.timegm(
80
+
81
+ datetime.datetime.utcnow().timetuple()),
82
+
83
+ "delkey": "hogefuga"}
84
+
85
+
86
+
87
+ class TestPost(TestCase):
88
+
89
+ client = MongoClient(conf._conf["address"], conf._conf["port"])
90
+
91
+ db = client[conf._conf["database"]]
92
+
93
+ collection = db[conf._conf["collection"]]
94
+
95
+
96
+
97
+ def setUp(self):
98
+
99
+ # とりあえず書く
100
+
101
+ self.testindex = TestPost.collection.insert_one(testdata)
102
+
103
+
104
+
105
+ def tearDown(self):
106
+
107
+ TestPost.collection.delete_one(self.testindex)
108
+
109
+
110
+
111
+ def test_make_model(self):
112
+
113
+ """ モデルが読み込まれてインスタンスが生成されるか """
114
+
115
+ model = models.Post()
116
+
117
+ model.read(self.testindex)
118
+
119
+
120
+
121
+ d = {"content": model.content,
122
+
123
+ "limit": model.limit,
124
+
125
+ "delkey": model.delkey}
126
+
127
+
128
+
129
+ del testdata['_id']
130
+
131
+
132
+
133
+ self.assertDictEqual(d, testdata)
134
+
135
+
136
+
137
+ def test_insert_model(self):
138
+
139
+ """ 生成したインスタンスが、インスタンスの情報を維持したまま挿入されるか """
140
+
141
+ model = models.Post()
142
+
143
+ model.content = "にくまん、あんまん、カレーまん"
144
+
145
+ model.delkey = "nununeno"
146
+
147
+ sample_id = model.post()
148
+
149
+
150
+
151
+ model_sample = models.Post.read(sample_id)
152
+
153
+
154
+
155
+ self.assertDictEqual(model, model_sample)
156
+
157
+
158
+
159
+
160
+
161
+ def test_delete_model(self):
162
+
163
+ """ 狙ったレコードがパスワードが合致した場合に削除されるか """
164
+
165
+ model = models.Post(self.testindex)
166
+
167
+ model.delete(testdata["delkey"])
168
+
169
+
170
+
171
+ self.assertRaises(NoneRecordException,
172
+
173
+ self.collection.find_one({"unique_id": testdata["unique_id"]}))
174
+
175
+
176
+
177
+ def test_dead_model(self):
178
+
179
+ """ リミットオーバーした場合投稿が削除されるか """
180
+
181
+ model = models.Post(self.testindex)
182
+
183
+ model.apoptosis()
184
+
185
+
186
+
187
+ self.assertRaises(NoneRecordException,
188
+
189
+ self.collection.find_one({"unique_id": testdata["unique_id"]}))
190
+
191
+
192
+
193
+ if __name__ == "__main__":
194
+
195
+ unittest.main()
196
+
197
+ ```
198
+
199
+
200
+
201
+ ```python
202
+
203
+ from conf import _conf
204
+
205
+ from pymongo import MongoClient
206
+
207
+ import json
208
+
209
+ import datetime
210
+
211
+ import random
212
+
213
+ import bson
214
+
215
+
216
+
217
+
218
+
219
+ class Post():
220
+
221
+ # DB Connection
222
+
223
+ connect = MongoClient(_conf["address"], _conf["port"])
224
+
225
+ posts = connect[_conf["database"]][_conf["collection"]]
226
+
227
+
228
+
229
+ def __init__(self):
230
+
231
+ # Read DB
232
+
233
+ self._id = None
234
+
235
+ self.content = ""
236
+
237
+ self.limit = 0.0
238
+
239
+ self.delkey = ""
240
+
241
+
242
+
243
+ def read(self, _id):
244
+
245
+ # Read DB
246
+
247
+ self._id = _id
248
+
249
+ post = Post.posts.find_one({"_id": self._id})
250
+
251
+ self.content = post["content"]
252
+
253
+ self.limit = post["limit"]
254
+
255
+ self.delkey = post["delkey"]
256
+
257
+
258
+
259
+ def post(self, content="", delkey=""):
260
+
261
+ self.content = content
262
+
263
+ self.delkey = delkey
264
+
265
+ if self.delkey == "":
266
+
267
+ self.delkey = make_delkey()
268
+
269
+ self.limit = datetime.timedelta(seconds=28800)
270
+
271
+
272
+
273
+ # Writing DB
274
+
275
+ try:
276
+
277
+ result = Post.posts.insert_one({"content": self.content,
278
+
279
+ "limit": self.limit,
280
+
281
+ "delkey": self.delkey})
282
+
283
+ return str(result["_id"])
284
+
285
+ except Exception as e:
286
+
287
+ return e
288
+
289
+
290
+
291
+ def delete(self, delkey):
292
+
293
+ try:
294
+
295
+ if self._id == _id and self.delkey == delkey:
296
+
297
+ # Remove post in DB
298
+
299
+ collection.delete_one({"_id": self._id})
300
+
301
+ return True
302
+
303
+ else:
304
+
305
+ return False
306
+
307
+ except Exception as e:
308
+
309
+ return e
310
+
311
+
312
+
313
+ def make_delkey(length=6):
314
+
315
+ # Make font map
316
+
317
+ alphabets = []
318
+
319
+ codes = (('a', 'z'), ('A', 'Z'), ('0', '9'))
320
+
321
+ for r in codes:
322
+
323
+ chars = map(chr, range(ord(r[0]), ord(r[1]) + 1))
324
+
325
+ alphabets.extend(chars)
326
+
327
+
328
+
329
+ password = [random.choice(alphabets) for _ in range(length)]
330
+
331
+ delkey = "".join(password)
332
+
333
+
334
+
335
+ return delkey
336
+
337
+
338
+
339
+ if __name__ == "__main__":
340
+
341
+ test = Post()
342
+
343
+
344
+
345
+ test.connect = "にくまん"
346
+
347
+ test.delkey = "hogefuga"
348
+
349
+ test.limit = 123456.78
350
+
351
+
352
+
353
+ print(test.post())
354
+
355
+ ```
48
356
 
49
357
 
50
358
 

1

初心者アイコンを取り消し。

2016/03/06 08:03

投稿

manzyun
manzyun

スコア2244

test CHANGED
File without changes
test CHANGED
File without changes