Adding the first test to `rxpy3-examples`


Since I went through the effort of publishing my rxpy3-examples repository on GitHub, I thought it’s time to add tests.

I had already taken a look at the test-suite for the rxpy-library, thinking that I might get some inspiration from that. But looking at their code I quickly found that it is way too complicated to be helpful to a rxpy-beginner like myself. They have created their own DSL for test, which is fine if you work a project over long period of time. But if you want to learn how to use the library as a beginner – like myself – then that is an additional barrier to entry.

That’s why I decided that my tests should be very, very easy to understand.

Basically, the idea is this: All my examples will generate some output, so I want to write my tests in such a way that it’s trivial to see what the expected output is. The assertion should look something like asssert output == expected_output.

The actual implementation now looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from examples.rx3_buffer_example import buffer_example
from tests.utils import capture_stdout_as_list

expected_output = """Subscribing to source
action before buffering 0
action before buffering 1
action before buffering 2
buffer marker 0
action after buffering [0, 1, 2]
action before buffering 3
action before buffering 4
action before buffering 5"""


def test_buffer_example():
    # act
    with capture_stdout_as_list() as output:
        buffer_example()

    # assert
    assert output.to_string() == expected_output

  • exptected_output is what the name implies: the expected output. I am using a multi-line string because it makes copying and pasting the output very easy, even though it looks ugly
  • capture_stdout_as_list is a context manager that captures the sys.stdout and provides a to_string() method that joins the list using the system’s line-separator

Of course there is room for improvement, but it’s a start. You can find my rxpy3-examples repository on GitHub.


See also