Top Banner
copyright(c) 2011 kuwata-lab.com all rights reserved. Oktest a new style testing library for Python makoto kuwata http://www.kuwata-lab.com/ PyCon JP 2011 LT 1
27

Oktest - a new style testing library for Python -

May 09, 2015

Download

Technology

kwatch

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.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Oktest - a new style testing library for Python -

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

Page 2: Oktest - a new style testing library for Python -

copyright(c) 2011 kuwata-lab.com all rights reserved.

unittest (python)

2

Page 3: Oktest - a new style testing library for Python -

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

Page 4: Oktest - a new style testing library for Python -

copyright(c) 2011 kuwata-lab.com all rights reserved.

Test::More (perl)

4

Page 5: Oktest - a new style testing library for Python -

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

Page 6: Oktest - a new style testing library for Python -

copyright(c) 2011 kuwata-lab.com all rights reserved.

Oktest (python)

6

Page 7: Oktest - a new style testing library for Python -

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

Page 8: Oktest - a new style testing library for Python -

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

Page 9: Oktest - a new style testing library for Python -

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

Page 10: Oktest - a new style testing library for Python -

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

Page 11: Oktest - a new style testing library for Python -

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

Page 12: Oktest - a new style testing library for Python -

copyright(c) 2011 kuwata-lab.com all rights reserved.

unittest (python)

12

Page 13: Oktest - a new style testing library for Python -

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

Page 14: Oktest - a new style testing library for Python -

copyright(c) 2011 kuwata-lab.com all rights reserved.

Oktest (python)

14

Page 15: Oktest - a new style testing library for Python -

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

Page 16: Oktest - a new style testing library for Python -

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

Page 17: Oktest - a new style testing library for Python -

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

Page 18: Oktest - a new style testing library for Python -

copyright(c) 2011 kuwata-lab.com all rights reserved.

Fixture Injectionin Oktest

18

Page 19: Oktest - a new style testing library for Python -

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

Page 20: Oktest - a new style testing library for Python -

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

Page 21: Oktest - a new style testing library for Python -

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

Page 22: Oktest - a new style testing library for Python -

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

Page 23: Oktest - a new style testing library for Python -

copyright(c) 2011 kuwata-lab.com all rights reserved.

Interested?

23

Page 24: Oktest - a new style testing library for Python -

copyright(c) 2011 kuwata-lab.com all rights reserved.

Oktest Python Google Search

24

Page 25: Oktest - a new style testing library for Python -

copyright(c) 2011 kuwata-lab.com all rights reserved.

one more thing...

25

Page 26: Oktest - a new style testing library for Python -

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

Page 27: Oktest - a new style testing library for Python -

copyright(c) 2011 kuwata-lab.com all rights reserved.

thank you

27