http://www.perlmonks.org?node_id=11129174


in reply to Re^2: Introspecting function signatures
in thread Introspecting function signatures

I had a quick glance at that repo, and have no idea what it does apart from the fact that it's supposed to be similar to Pytest, about which I know nothing. So could you explain further the use case?

Dave.

  • Comment on Re^3: Introspecting function signatures

Replies are listed 'Best First'.
Re^4: Introspecting function signatures
by szabgab (Priest) on Mar 06, 2021 at 13:57 UTC
    It is an experiment to write a module to allow you to write tests like this:
    sub test_someting($tempdir) { ... }
    The test harness runs all the individual test_something functions. If the function has arguments that it will prepare the appropriate object and call the function passing those objects into the function. The example I created was injecting a $tempdir object, but, once the system is ready, you could write and register your own objects. In Pythest these are called fixtures, and they mostly act test fixtures. (In Pytest there are a few other ready-made such fixtures, for example one that captures stdout/stderr, and one that can mock methods, attributes, etc.)

    If you look at the modules in the t/ directory, those are all examples on how to use the module to write tests.

      Is there a reason why they need to be available as parameters at all? These fixtures could just be global variables, couldn't they?

        They could, but this way they are localized to the function, they go out of scope after the function is done and one test will not impact another test.
      I may have trouble understanding the full intention.... please help me:

      You want to parse

      sub test_someting($tempdir) { # do some tests }

      And because you see the (registered) keyword "tempdir" your test system is supposed to call this test with a prepared temporary directory?

      ok( test_something( create_tempdir() ), "something", )

      Kind of a naming convention?

      Did I get it right?

      If yes, I could think of some alternative (and more perlish) ways to achieve this without needing to parse the signature.

      PS: not sure why you call this "dependency injection" or do you plan testruns where the injected $tempdir is not a temporary dir? :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Yes. "dependency injection" is how this is called in the case of pytest, maybe it is not a generic term. You could create your own "fixture" and then you or others could set its name as a parameter.

        I put together an example of creating and registering a fixture and then also use that fixture.

        I would love to see your suggestion for more Perlish way.