oktest - a new style testing library for python -
DESCRIPTION
Oktest is a new-style testing library for Python. It helps you to read & write tests very much. Oktest is available with (or without) standard 'unittest' module.TRANSCRIPT
copyright(c) 2011 kuwata-lab.com all rights reserved.
Oktesta new style testing library for Python
makoto kuwatahttp://www.kuwata-lab.com/
PyCon JP 2011 LT
1
copyright(c) 2011 kuwata-lab.com all rights reserved.
unittest (python)
2
copyright(c) 2011 kuwata-lab.com all rights reserved.
import unittest
class MyTC(unittest.TestCase):
def test_1_plus_1(self): """1+1 should be 2""" self.assertEqual(2, 1+1)
Too long! Hard to read & write!長すぎ! 読みにくいし書きにくい!
3
copyright(c) 2011 kuwata-lab.com all rights reserved.
Test::More (perl)
4
copyright(c) 2011 kuwata-lab.com all rights reserved.
use Test::More;
ok(1+1 == 2);is(1+1, 2);eq($actual, $expected);
Easy to read & write! Kool! 読むのも書くのも楽! これはイケてる!
5
copyright(c) 2011 kuwata-lab.com all rights reserved.
Oktest (python)
6
copyright(c) 2011 kuwata-lab.com all rights reserved.
import unittestfrom oktest import ok
class MyTC(unittest.TestCase): def test_1_plus_1(self): ok (1+1) == 2 ok (1+1) != 0
So Kooooooool!これぞスクリプト言語!
7
copyright(c) 2011 kuwata-lab.com all rights reserved.
## Oktest ## unittestok (s) == 'foo' self.assertEqual(s, 'foo')ok (s) != 'foo' self.assertNotEqual(s, 'foo')ok (n) > 0 self.assert_(n > 0)ok (fn).raises(Error) self.assertRaises(Error, fn)ok ([]).is_a(list) self.assertIsInstance([], list)NG ([]).is_a(tuple) self.assertNotIsInstance([], tuple)ok ('file').is_file() self.assert_(os.path.isfile('file'))NG ('file').is_dir() self.assert_(not os.path.isdir('file'))
Not kool. It's Java.Javaっぽくてイケてない
Kool Python!Pythonらしい、イケてるコード
8
copyright(c) 2011 kuwata-lab.com all rights reserved.
>>> ok (1+1)<oktest.AssertionObject object>
>>> ok (1+1).__eq__(1)Traceback (most recent call last): ...AssertionError: 2 == 1 : failed.
ok() returns AssertionObjectok() は AssertionObject を返す
Overrides '==' operator'==' 演算子をオーバーライドしてる
9
copyright(c) 2011 kuwata-lab.com all rights reserved.
$ python foo_test.py..F...=============================================FAIL: test1 (__main__.FooTest)---------------------------------------------Traceback (most recent call last): File "foo_test.py", line 14, in test1 ok (s1) == s2AssertionError: actual == expected: failed.--- expected+++ actual@@ -1,3 +1,3 @@ AAA+BBB CCC-DDD
Shows unified diff when '==' is failed.'==' が失敗したら uniifed diff を表示 (2.6以下でも使えるよ!)
10
copyright(c) 2011 kuwata-lab.com all rights reserved.
## Python AssertionError:assert x > y (no message)
## Nose AssertionError:ok_(x > y) (no message)
## Oktest AssertionError:ok (x) > y 1 > 2 : failed
Shows actual & expected values失敗時に、実際値と期待値を表示してくれる
Test Code When Failed
11
copyright(c) 2011 kuwata-lab.com all rights reserved.
unittest (python)
12
copyright(c) 2011 kuwata-lab.com all rights reserved.
import unittest
class MyTC(unittest.TestCase):
def test_1_plus_1_eq_2(self): """1 + 1 should be 2""" self.assertEqual(2, 1+1)
Duplicated. Not DRY!メソッド名と同じこと書くならDRYじゃないよね
You must describe in [a-zA-Z0-9_]+(通常は)アルファベット、数字、_ でしか書けない
13
copyright(c) 2011 kuwata-lab.com all rights reserved.
Oktest (python)
14
copyright(c) 2011 kuwata-lab.com all rights reserved.
import unittestfrom oktest import ok, test
class MyTC(unittest.TestCase):
@test("1+1 should be 2") def _(self): ok (1+1) == 2
Free text instead of method name!制限のあるメソッド名ではなく普通の文字列で書ける!
15
copyright(c) 2011 kuwata-lab.com all rights reserved.
import unittestfrom oktest import ok, test
class MyTC(unittest.TestCase):
@test("1+1は2になるべし") def _(self): ok (1+1) == 2
Any symbol or CJK available!どんな記号も、日本語でも大丈夫、問題ない
16
copyright(c) 2011 kuwata-lab.com all rights reserved.
@test("1+1 should be 2")def _(self): ...
_.__doc__ = "1 + 1 should be 2"_.__name__ = "test_" + "1 + 1 should be 2"locals()[_.__name__] = _
Set free text as method name,keeping with unittest compatibilityunittestと互換性を保ったまま、記号や空白を含む
ような文字列をメソッド名として設定
internal
17
copyright(c) 2011 kuwata-lab.com all rights reserved.
Fixture Injectionin Oktest
18
copyright(c) 2011 kuwata-lab.com all rights reserved.
class SosTest(unittest.TestCase):
def setUp(self): self.member = Member.find(id=1) self.team = Team.find(name="sos")
def test_member(self): ok (self.member.name) == "Haruhi"
def test_team(self): ok (self.team.name) == "SOS"
All test methods call the same 'setUp()'. Coarse-grained.Want to call different initializer for each test method.すべてのテストメソッドが同じ setUp() を呼び出す。これは粒度が荒すぎる。
テストメソッドごとに違う初期化処理を呼べるようにしたい。
19
copyright(c) 2011 kuwata-lab.com all rights reserved.
class SosTest(unittest.TestCase):
def provide_member(self): return Member.find(id=1) def provide_team(self): return Team.find(name="sos")
def test_member(self, member): ok (member.name) == "Haruhi" def test_team(self, team): ok (team.name) == "SOS"
Calls provide_member() only
Calls provide_team() only
20
copyright(c) 2011 kuwata-lab.com all rights reserved.
class InjectionTest(unittest.TestCase):
def provide_A(B,C): return ["a"]+b+c def provide_B(): return ["b"] def provide_C(D): return ["c"]+d def provide_D(): reutrn ["d"]
def test_dependency(self, A): ok (A) == ["a", "b", "c", "d"]
- 'A' depends on 'B' and 'C'- 'C' depends on 'D'
AはBとCに依存し、CはDに依存する
Dependencies are resolved automatically依存性はOktestにより自動的に解決される
21
copyright(c) 2011 kuwata-lab.com all rights reserved.
class FileTest(unittest.TestCase):
def provide_dummy(self): filename = "foo.txt" with open(filename, 'w') as f: f.write("foobar\n") return filename
def release_dummy(self, filename): if os.path.exists(filename): os.unlink(filename)
Equivarent to setUp()
Equivarent to tearDown()
provide_xxx()がsetUp()相当
release_xxx()がtearDown()相当
22
copyright(c) 2011 kuwata-lab.com all rights reserved.
Interested?
23
copyright(c) 2011 kuwata-lab.com all rights reserved.
Oktest Python Google Search
24
copyright(c) 2011 kuwata-lab.com all rights reserved.
one more thing...
25
copyright(c) 2011 kuwata-lab.com all rights reserved.
var oktest = require('oktest'), topic = oktest.topic, spec = oktest.spec, ok = oktest.ok;
topic("ClassName", function() { topic(".methodName()", function() { spec("...description...", function() { ok (1+1).eq(2); ok (1+1, '==', 2); }); });});
if (process.argv[1] === __filename) oktest.main();
Oktest for Node.js
## install$ npm install oktest
26
copyright(c) 2011 kuwata-lab.com all rights reserved.
thank you
27