Skip to content

Jmw/use pytest#2

Open
Business-Wizard wants to merge 4 commits intomainfrom
jmw/use-pytest
Open

Jmw/use pytest#2
Business-Wizard wants to merge 4 commits intomainfrom
jmw/use-pytest

Conversation

@Business-Wizard
Copy link
Copy Markdown
Owner

No description provided.

Comment thread test_bottles.py

def test_the_whole_song(self):
expected = """99 bottles of beer on the wall, 99 bottles of beer.
def test_the_first_verse():
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Later in the book we will be putting test suites for different classes in the same file. Is there a way of creating a separate namespace for the tests using Pytest?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - pytest enables grouping tests via classes and modules (files).
I believe you should be able to group them into the TestClasses to avoid namespace issues.

def test_method_a():
    setup_var = 5
    object = main.my_class()

    actual = object.act(setup_var)
    expected = 6

    assert actual == expected


def test_method_b():
    setup_var = 5
    object = main.my_class()

    actual = object.send(setup_var)
    expected = 6

    assert actual == expected
class TestMyClass:
    def test_method_a(self):
        setup_var = 5
        object = main.my_class()

        actual = object.act(setup_var)
       expected = 6

        assert actual == expected

    def test_method_b(self):
        setup_var = 5
        object = main.my_class()

        actual = object.send(setup_var)
        expected = 6

        assert actual == expected

Comment thread test_bottles.py
'Take one down and pass it around, '
'98 bottles of beer on the wall.\n'
)
assert Bottles().verse(99) == expected
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine to me, but I don't see why this is better than unittest. They're both equally readable, and one is not more verbose than the other.

Aside from "most people use pytest", is there another compelling reason to choose that over unittest?

Copy link
Copy Markdown
Owner Author

@Business-Wizard Business-Wizard Jan 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my experience teaching Python beginners as well as working in our industry, Pytest should be chosen over unittest by default. Outside of academia (not their job to code well) and folks coming from other languages (Java), I haven't seen unittest used. My hunch is it's more of a case of those groups not being aware of pytest to start out with. 🤷‍♂️

Some high-level items to consider:

  • most developers will know Pytest -> easier read for the market audience
  • inexperienced readers will have an easier learning curve (eg no need for multiple assert methods)
  • After some sleep, I think it unlikely we'll be pulling out parametrize or a fixture in the book 😆 the modern pytest features may come in handy - marks, fixtures, parameterize
  • despite being written in Python overall, the use of unittest would yet stymie my confidence in recommending this book or leveraging it in future teaching opportunities. 😢

Copy link
Copy Markdown

@kytrinyx kytrinyx Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember that this book is explicitly not for Python beginners. It's for Python developers who are new to OOP. We're explicitly not teaching Python.

Outside of academia (not their job to code well) and folks coming from other languages (Java), I haven't seen unittest used

This is probably the best argument, to me. If it's what people expect, then using something else will cause friction. /shrug

It's like using the wrong enumerable method for the job in Ruby.

the modern pytest features may come in handy - marks, fixtures, parameterize

Do you mean that it will come in handy in the context of the book? Can you point out exactly where in the book Pytest will be better than unittest?

no need for multiple assert methods

We only ever use assertEqual in the book.

Despite not seeing the "easier to read" thing myself (they look exactly the same to me) I will switch to using Pytest on the strength of the first argument.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't expect any of these to be used in the book's examples, but in case a brief overview is helpful here are the three features I mentioned. While I don't foresee a use-case, maybe you will 😊 🤷‍♂️

  • fixtures: the streamlined way to provide setup for tests while ensuring clean teardowns when tests complete or fail
    • examples: create an infrastructure such as a database or filesystem, provide a Stub/Mock system to avoid I/O calls, or simply do a complex setup for a more hairy test (many required args perhaps)
  • parametrize: de-duplicate tests. It's like meta-creating them on the fly, or defining them as a product
    • example: instead of writing a test for each possible value of arg, define the possible values and let it make a test for each pair of possibilities
  • marks: allow you to group and run tests by a category, separate from the behaviour or module they are defined for.
    • examples: tests that are slow-running, require network access, take up large resources, etc

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Yeah, I don't see any of this coming up in the context of 99 bottles :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants