-
-
Notifications
You must be signed in to change notification settings - Fork 402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add generation of unit tests from examples #297
Conversation
Example of running the tests from command line with 4 processes. Right now running without multithreading is faster, but I suspect that will change once more tests are added to modules that talk scrape web sites and such.
|
I would much rather have most test related code outside the main package. is that possible? |
Merge with tests instead? |
Not my point. My point is that I don't like having to have custom code in the modules to have them included in the tests, and I don't like those special classes being in module and bot. But my opinion is not relevant here. |
@elad661 no custom code is required in the modules themselves to have them included in the tests, only the example decorator with the extra result parameter. The run_example_tests is completely optional, but I added it because it seems useful to test modules as you change them. It's also a good tool for developing new modules. It simply calls pytest with the appropriate arguments, so that you don't have to drop into command line and figure out how to do it your self. As for the mock classes and helper code for run_example_tests, that could of course be put into a separate module. I put the mock classes in tools originally, but there was a cyclic import problem thanks to all the "from tools import ..." statements (plain import doesn't require the module to be fully initialized), which is why I relocated them to bot. |
-Add new module test_tools. -Change the structure of callables example attribute to be a list of dicts. -Move generation of command-regexps to tools.
Conflicts: willie/tools.py
Add generation of unit tests from examples
This implements functionality described in issue #292. It makes
@example
inject test-functions into the module, which testing frameworks can then find. I also added a helper function to willie.module that calls pytest with appropriate parameters. The idea is that all modules would haveif name == "__main__": willie.module.run_example_tests(__file__)
at the bottom.Right now
@example
with just one parameter works as before, but if you give a second parameter a test is added, where the second parameter is the expected result. If more than one example is given, a test is made for each of them.Usage requires pytest and pytest-xdist to be installed. I also tested the code coverage plugin pytest-cov and it worked wonderfully.
I added two new classes to bot.Willie: MockWillie and MockWillieWrapper. They are still a bit unfinished. I will add more functionality as needed when converting modules to use this.
I'm adding this as a pull request as I made a bunch of changes and it needs a bit more testing.