testing command line utilities

后端 未结 4 2138
青春惊慌失措
青春惊慌失措 2021-02-14 02:01

I\'m looking for a way to run tests on command-line utilities written in bash, or any other language.

I\'d like to find a testing framework that would have statements li

4条回答
  •  天涯浪人
    2021-02-14 02:17

    I know that this question is old, but since I was looking for an answer, I figured I would add my own for anyone else who happens along.

    Full disclaimer: The project I am mentioning is my own, but it is completely free and open source.

    I ran into a very similar problem, and ended up rolling my own solution. The test code will look like this:

    from CLITest import CLITest, TestSuite
    from subprocess import CalledProcessError
    
    
    class TestEchoPrintsToScreen(CLITest):
        '''Tests whether the string passed in is the string
        passed out'''
    
        def test_output_contains_input(self):
            self.assertNotIsInstance(self.output, CalledProcessError)
            self.assertIn("test", self.output)
    
        def test_ouput_equals_input(self):
            self.assertNotIsInstance(self.output, CalledProcessError)
            self.assertEqual("test", self.output)
    
    suite = TestSuite()
    
    suite.add_test(TestEchoPrintsToScreen("echo test"))
    
    suite.run_tests()
    

    This worked well enough to get me through my issues, but I know it could use some more work to make it as robust as possible (test discovery springs to mind). It may help, and I always love a good pull request.

提交回复
热议问题