<div dir="ltr">in the 'better' example, i would remove usage of the class</div><div class="gmail_extra"><br><br><div class="gmail_quote">On 2 September 2014 17:58, Bruno Oliveira <span dir="ltr"><<a href="mailto:nicoddemus@gmail.com" target="_blank">nicoddemus@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Sep 2, 2014 at 10:14 AM, Laszlo Papp <span dir="ltr"><<a href="mailto:lpapp@kde.org" target="_blank">lpapp@kde.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> n</blockquote></div><br>Hi Laszlo,</div><div class="gmail_extra">
<br></div><div class="gmail_extra">People at work have asked me the same question, and for people just getting their feet wet is usually better to introduce pytest features as close to what they're accustomed to as possible, so their learning curve can be smoother.</div>
<div class="gmail_extra"><br></div><div class="gmail_extra">Everyone, in your opinion, what would be a good example that uses pytest fixtures but doesn't diverge too much what someone might be used to `XUnit`? I like Laszlo example, but I would change it to not use a class-scoped fixture and perhaps use `yield_fixture` to show off how easy it is to write tear-down code.</div>
<div class="gmail_extra"><br></div><div class="gmail_extra">So this `XUnit` setup code:</div><div class="gmail_extra"><br></div><div class="gmail_extra">```python</div><div class="gmail_extra"><div class="gmail_extra"><div class="gmail_extra">
<div class="gmail_extra">class Test(TestCase):</div><div class="gmail_extra"><br></div><div class="gmail_extra"> def setUp(self):</div><div class="gmail_extra"> self.user = User('test-user', 'password')</div>
<div class="gmail_extra"> self.user.register()</div><div class="gmail_extra"> self.session = Session() </div><div class="gmail_extra"> self.session.login(self.user)</div><div class="gmail_extra">
</div><div class="gmail_extra"> def tearDown(self):</div><div class="gmail_extra"> self.session.logout()</div><div class="gmail_extra"> self.user.unregister()</div><div class="gmail_extra"> </div>
<div class="gmail_extra"> def test_current_user(self): </div><div class="gmail_extra"> self.assertEqual(self.session.get_current_user().name, <a href="http://self.user.name" target="_blank">self.user.name</a>) </div>
</div></div></div><div class="gmail_extra">```<br></div><div class="gmail_extra"><br></div><div class="gmail_extra">Can be almost directly translated to use py.test fixtures like this:</div><div class="gmail_extra"><br></div>
<div class="gmail_extra">```python</div><div class="gmail_extra"><div class="gmail_extra">@pytest.yield_fixture </div><div class="gmail_extra">def fixture():</div><div class="gmail_extra"> class Fixture: pass </div>
<div class="gmail_extra"> f = Fixture()</div><div class="gmail_extra"> f.user = User('test-user', 'password')</div><div class="gmail_extra"> f.user.register()</div><div class="gmail_extra"> f.session = Session() </div>
<div class="gmail_extra"> f.session.login(self.user)</div><div class="gmail_extra"> yield f</div><div class="gmail_extra"> f.session.logout()</div><div class="gmail_extra"> f.user.unregister()</div><div class="gmail_extra">
<br></div><div class="gmail_extra"> </div><div class="gmail_extra">class Test(object):</div><div class="gmail_extra"> </div><div class="gmail_extra"> def test_current_user(fixture): </div><div class="gmail_extra">
assert fixture.session.get_current_user().name == <a href="http://fixture.user.name" target="_blank">fixture.user.name</a></div></div><div class="gmail_extra">```<br></div><div class="gmail_extra"><br></div><div class="gmail_extra">
Which can then be further improved to show off how fixtures can reuse other fixtures:</div><div class="gmail_extra"><br></div><div class="gmail_extra">```python </div><div class="gmail_extra"><div class="gmail_extra">
@pytest.yield_fixture </div><div class="gmail_extra">def logged_session(user): </div><div class="gmail_extra"> session = Session() </div><div class="gmail_extra"> session.login(self.user)</div>
<div class="gmail_extra"> yield session</div><div class="gmail_extra"> session.session.logout()</div><div class="gmail_extra"><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">@pytest.yield_fixture </div>
<div class="gmail_extra">def user(): </div><div class="gmail_extra"> user = User('test-user', 'password')</div><div class="gmail_extra"> user.register()</div><div class="gmail_extra"> yield user</div>
<div class="gmail_extra"> user.unregister()</div><div class="gmail_extra"> </div><div class="gmail_extra">class Test(object):</div><div class="gmail_extra"> </div><div class="gmail_extra"> def test_current_user(session, user): </div>
<div class="gmail_extra"> assert session.get_current_user().name == <a href="http://user.name" target="_blank">user.name</a></div></div><div class="gmail_extra">```<br></div><div class="gmail_extra"><br></div><div class="gmail_extra">
The idea here is to allow the user to map what he already knows, and gradually introduce fixtures as a concept to make it easier to reuse setup/tear down code in a more modular fashion.</div><div class="gmail_extra">And furthermore the example can be improved by for example parameterizing the `user` fixture with the original `"test-user"` and another `"anonymous"` user without password, producing new tests automatically.</div>
<div class="gmail_extra"><br></div><div class="gmail_extra">Any thoughts?</div><div class="gmail_extra"><br></div><div class="gmail_extra"><br></div></div>
<br>_______________________________________________<br>
Pytest-dev mailing list<br>
<a href="mailto:Pytest-dev@python.org">Pytest-dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/pytest-dev" target="_blank">https://mail.python.org/mailman/listinfo/pytest-dev</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div dir="ltr">Anatoly Bubenkov<br></div>
</div>