通过使用假设附加包Faker,我遇到了 python 单元测试的问题。我想测试我网站的登录过程,我已经有了单元测试场景,但我想用假设自动化场景。这是我用于电子邮件和密码验证的简单代码。
import unittest
from Environtments.ParametrizedTestCase import ParametrizedTestCase
from hypothesis import given
from faker import Faker
class TestLogin(ParametrizedTestCase):
fake = Faker('en_US')
@given(email = fake.email(),password = fake.password()
def test_login(self,email,password):
assert '@' in email
assert len(password) >= 6
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(ParametrizedTestCase.parametrize(TestLogin, param=""))
unittest.TextTestRunner(verbosity=2).run(suite)
我总是发现这个错误。有什么我错过的吗?
test_login (__main__.TestLogin) ... You can add @seed(311936867547523412638507752560457398354) to this test to reproduce this failure.
ERROR
======================================================================
ERROR: test_login (__main__.TestLogin)
----------------------------------------------------------------------
Traceback (most recent call last):
File "tester_uf_exmp_hypo.py", line 10, in test_login
def test_login(self,email,password):
File "/Library/Python/2.7/site-packages/hypothesis/core.py", line 1001, in wrapped_test
state.run()
File "/Library/Python/2.7/site-packages/hypothesis/core.py", line 725, in run
runner.run()
File "/Library/Python/2.7/site-packages/hypothesis/internal/conjecture/engine.py", line 435, in run
self._run()
File "/Library/Python/2.7/site-packages/hypothesis/internal/conjecture/engine.py", line 771, in _run
self.reuse_existing_examples()
File "/Library/Python/2.7/site-packages/hypothesis/internal/conjecture/engine.py", line 626, in reuse_existing_examples
self.test_function(last_data)
File "/Library/Python/2.7/site-packages/hypothesis/internal/conjecture/engine.py", line 153, in test_function
self._test_function(data)
File "/Library/Python/2.7/site-packages/hypothesis/core.py", line 691, in evaluate_test_data
escalate_hypothesis_internal_error()
File "/Library/Python/2.7/site-packages/hypothesis/core.py", line 663, in evaluate_test_data
result = self.execute(data, collect=True)
File "/Library/Python/2.7/site-packages/hypothesis/core.py", line 578, in execute
result = self.test_runner(data, run)
File "/Library/Python/2.7/site-packages/hypothesis/executors.py", line 58, in default_new_style_executor
return function(data)
File "/Library/Python/2.7/site-packages/hypothesis/core.py", line 552, in run
args, kwargs = data.draw(self.search_strategy)
File "/Library/Python/2.7/site-packages/hypothesis/internal/conjecture/data.py", line 158, in draw
return self.__draw(strategy, label=label)
File "/Library/Python/2.7/site-packages/hypothesis/internal/conjecture/data.py", line 175, in __draw
return strategy.do_draw(self)
File "/Library/Python/2.7/site-packages/hypothesis/core.py", line 180, in do_draw
return self.base.do_draw(data)
File "/Library/Python/2.7/site-packages/hypothesis/searchstrategy/lazy.py", line 157, in do_draw
return data.draw(self.wrapped_strategy)
File "/Library/Python/2.7/site-packages/hypothesis/internal/conjecture/data.py", line 148, in draw
if strategy.is_empty:
File "/Library/Python/2.7/site-packages/hypothesis/searchstrategy/strategies.py", line 144, in accept
recur(self)
File "/Library/Python/2.7/site-packages/hypothesis/searchstrategy/strategies.py", line 141, in recur
mapping[strat] = getattr(strat, calculation)(recur)
File "/Library/Python/2.7/site-packages/hypothesis/searchstrategy/collections.py", line 67, in calc_is_empty
return any(recur(e) for e in self.element_strategies)
File "/Library/Python/2.7/site-packages/hypothesis/searchstrategy/collections.py", line 67, in <genexpr>
return any(recur(e) for e in self.element_strategies)
File "/Library/Python/2.7/site-packages/hypothesis/searchstrategy/strategies.py", line 141, in recur
mapping[strat] = getattr(strat, calculation)(recur)
File "/Library/Python/2.7/site-packages/hypothesis/searchstrategy/strategies.py", line 541, in calc_is_empty
return recur(self.mapped_strategy)
File "/Library/Python/2.7/site-packages/hypothesis/searchstrategy/strategies.py", line 141, in recur
mapping[strat] = getattr(strat, calculation)(recur)
File "/Library/Python/2.7/site-packages/hypothesis/searchstrategy/lazy.py", line 87, in calc_is_empty
return recur(self.wrapped_strategy)
File "/Library/Python/2.7/site-packages/hypothesis/searchstrategy/lazy.py", line 110, in wrapped_strategy
*self.__args, **self.__kwargs
File "/Library/Python/2.7/site-packages/hypothesis/strategies.py", line 651, in fixed_dictionaries
check_strategy(v)
File "/Library/Python/2.7/site-packages/hypothesis/internal/validation.py", line 45, in check_strategy
check_type(SearchStrategy, arg, name)
File "/Library/Python/2.7/site-packages/hypothesis/internal/validation.py", line 39, in check_type
% (typ_string, name, arg, type(arg).__name__))
InvalidArgument: Expected SearchStrategy but got u'2)Y4+APqu@' (type=unicode)
----------------------------------------------------------------------
Ran 1 test in 2.013s
FAILED (errors=1)
我将代码更改为此。
from hypothesis.extra.fakefactory import fake_factory
@given(email = fake_factory('email'),password = fake_factory('password'))
这是警告。但伪装者有效。这会是个问题吗?
tester_uf_exmp_hypo.py:9: HypothesisDeprecationWarning: hypothesis.extra.fakefactory has been deprecated, because it does not support example discovery or shrinking. Consider using a lower-level strategy (such as st.text()) instead.
@given(email = fake_factory('email'),password = fake_factory('password'))
test_login (__main__.TestLogin) ... kaareberge@pedersen.no
!l3QCGDd8I
myslrshd@bn.com
(@s(RwUM0z