Python de BDD (in Lettuce)

Installation

With pip as usual.

$ pip install lettuce

Write a feature

You can change the extension to .feature, but it feels better to write it in the features directory. It seems that it also supports Japanese.

features/example.feature


Feature: a cookie in a bug
  In order to eat a cookie
  I carry a bug

Scenario: simple test
  Given I have a bug
  Then a cookie exists in the bug

So, when you run lettuce, it will show you a template of the steps you need to write.

python


#You can implement step definitions for undefined steps with these snippets:

# -*- coding: utf-8 -*-
from lettuce import step

@step(u'Given I have a bug')
def given_i_have_a_bug(step):
    assert False, 'This step must be implemented'
@step(u'Then a coookie exists in the bug')
def then_a_cookie_exists_in_the_bug(step):
    assert False, 'This step must be implemented'

Is there no pending ...?

Write Step

Create it with .py in the same directory as the feature you created. First of all, copy.

features/example.py


# -*- coding: utf-8 -*-
from lettuce import step

@step(u'Given I have a bug')
def given_i_have_a_bug(step):
    assert False, 'This step must be implemented'
@step(u'Then a coookie exists in the bug')
def then_a_coookie_exists_in_the_bug(step):
    assert False, 'This step must be implemented'

If nothing is done, everything will fail, so rewrite it like that.

example.py


# -*- coding: utf-8 -*-
from lettuce import *
import sys
import bug #I will make it from now on

@step(u'Given I have a (.*)$')
def given_i_have_a_bug(step, arg):
    cls = getattr(sys.modules[arg], arg[0].upper() + arg[1:])
    world.__dict__[arg] = cls()
    assert world.__dict__[arg], arg + ' must be an instance'

@step(u'Then a cookie exists in the (.*)$')
def then_a_coookie_exists_in_the_bug(step, obj):
    assert world.__dict__[obj].cookie, arg1 + ' should return True'

test!

Just run lettuce.

Implementation

It's a little too shabby. .. ..

bug.py


class Bug(object):
  def __init__(self):
    self.cookie = True

Summarize

Recommended Posts

Python de BDD (in Lettuce)
Quadtree in Python --2
Python in optimization
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Sorted list in Python
Daily AtCoder # 36 in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 18 in Python
Singleton pattern in Python
File operations in Python
Key input in Python
Python de symmetric group 3
Daily AtCoder # 33 in Python
Logistic distribution in Python
Daily AtCoder # 7 in Python
LU decomposition in Python
One liner in Python
Daily AtCoder # 24 in Python
case class in python
RNN implementation in python
Daily AtCoder # 8 in Python
File processing in Python
Elasticsearch Reindex in Python
Daily AtCoder # 42 in Python
Basic sorting in Python