Skip to content
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

Merged
merged 5 commits into from
Jun 27, 2013
Merged

Add generation of unit tests from examples #297

merged 5 commits into from
Jun 27, 2013

Conversation

ari-koivula
Copy link
Contributor

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 have if 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.

@ari-koivula
Copy link
Contributor Author

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.

> python -m pytest willie\modules -n 4
============================= test session starts =============================
platform win32 -- Python 2.7.3 -- pytest-2.3.5
plugins: cov, xdist
gw0 [13] / gw1 [13] / gw2 [13] / gw3 [13]
scheduling tests via LoadScheduling
.............
========================== 13 passed in 2.57 seconds ==========================

@elad661
Copy link
Contributor

elad661 commented Jun 25, 2013

I would much rather have most test related code outside the main package. is that possible?

@tyrope
Copy link

tyrope commented Jun 25, 2013

Merge with tests instead?

@elad661
Copy link
Contributor

elad661 commented Jun 25, 2013

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.

@ari-koivula
Copy link
Contributor Author

@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
embolalia added a commit that referenced this pull request Jun 27, 2013
Add generation of unit tests from examples
@embolalia embolalia merged commit e8a8ab0 into master Jun 27, 2013
@ari-koivula ari-koivula deleted the example_tests branch June 27, 2013 20:22
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.

4 participants