开发者

Running nose with a large number of test-data files

I want to write some tests for a web scraper. I want to use a lot of test web pages, but I'm not sure exactly how to get nose (or another unit testing framework) to do what I need without a huge amount of duplicate code.

My problem is that I want to test a lot of different pages and I'm not sure how to do this using nose. This is roughly what I want to do:

class TestPage(object):
    def setup(self):
        with open('test_data/page.html', 'r') as f:
            html = f.read()
        self.scraper = Scraper(html)

This would be fine if the only page I wanted to test were 'page.html'. But I have hundreds of pages to test. I could duplicate the class and each time change both the class's name and the filename of th开发者_运维知识库e path, but this would obviously be ridiculous.

I thought of putting code in setup to create separate Scraper objects for each page and store them in a list in the test object. I could then have the test methods operate on each Scraper object. But I think I'd run into problems with keeping each test isolated and getting separate messages from nose.

I also tried to subclass a base test class and pass the path to init, but this creates problems for nose.

I'd appreciate any advice on how to solve this using nose, another approach to take, or any reading that might be useful.


based on your code sample, you just need a class factory:

def make_test(page)
    class TestPage(object):
        def setup(self):
            with open(page, 'r') as f:
                html = f.read()
            self.scraper = Scraper(html)
     return TestPage

Now you can just run over a list of pages and make one test for each one:

for page in list_of_pages:
    Test = make_test(page)
    Test().run() 

I'm not sure if that's how you run a nose test but It would be a full fledged class so you could do whatever you would normally do with it.

You could store all of your tests pages in one directory so that you could just loop over the files in the directory and get your list of pages that way. All you would have to do to create a new test is save the html in the given directory. Is that about what you were looking for?


Using Freshen (https://github.com/rlisagor/freshen) via nose seems to be the best answer.

Just write feature files, then you just have to add a scenario per URL to test.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜