cursor icon indicating copy to clipboard operation
cursor copied to clipboard

AI Test Generation

Open MrDevel0per opened this issue 2 years ago • 4 comments

The AI is easily able to generate tests for a given file with the Chat option. I am suggesting that Cursor adds a new feature to take this one step further - not only generating the code, but generating actual test files. Maybe a keyboard shortcut to generate tests for the given file with the AI?

MrDevel0per avatar Mar 27 '23 19:03 MrDevel0per

for now it will generate content starts where the cursor is, what ever it's been, event in a keyword. that's not that AI style...

iptton avatar Mar 31 '23 02:03 iptton

cool idea! what sorts of projects are you interested in generating tests for? How many tests do you want to generate at once?

Screenshots of things you want to tests / tests youd want the ai to generate are super helpful for figuring out how we should make this feature

truell20 avatar Apr 08 '23 08:04 truell20

@truell20 I mostly work in Swift & Python - I have several libraries that, for example, involve parsing. A lot of these files are interconnected - maybe some sort of code search feature would be necessary to give the AI the subdetails it needs to generate tests? For example, from a package I wrote to interact with the ChatGPT API, I have a class called APIResponse. This involves parsing the response from the web server, and can fail - I would want the AI to generate tests for this parsing. A screenshot of this file: Screenshot 2023-04-12 at 5 50 34 PM As you can see, the file depends on several other classes that it contains arrays of - so the AI would need to know those classes as well (they are declared in other files) in order to be able to generate the tests. For reference, here is the text of that file:

import Foundation

///A response from the OpenAI completions API.
public struct APIResponse: Decodable {
    ///The identifier used to identify this response
    public var id: String
    ///The type this response concerns. Will likely be `"messages"`
    public var object: String
    ///When this response was created (timeIntervalSince1970
    public var created: Date
    
    ///The model usage.
    public var usage: Usage
    ///The choices made by the model.
    public var choices: [Choice]
    
    
    
    ///The model name used for the completion
    public var model: String
    
    private enum CodingKeys: String, CodingKey {
           case id, object, created, model, usage, choices
       }
    
    public init(from decoder: Decoder) throws {
        // print("Starting to decode")
           let container = try decoder.container(keyedBy: CodingKeys.self)
        // print("Created container")
        self.id = try container.decode(String.self, forKey: .id)
        // print("Decoded id")
        self.object = try container.decode(String.self, forKey: .object)
        // print("Decoded object")
        self.model = try container.decode(String.self, forKey: .model)
        // print("Decoded model")
        self.usage = try container.decode(Usage.self, forKey: .usage)
        // print("Decoded usage")
        var choicesContainer = try container.nestedUnkeyedContainer(forKey: .choices)
                var choices: [Choice] = []
                while !choicesContainer.isAtEnd {
                    let choiceContainer = try choicesContainer.nestedContainer(keyedBy: Choice.CodingKeys.self)
                    let messageContainer = try choiceContainer.nestedContainer(keyedBy: Message.CodingKeys.self, forKey: Choice.CodingKeys.messages)
                    let role = try messageContainer.decode(String.self, forKey: Message.CodingKeys.role)
                    let content = try messageContainer.decode(String.self, forKey: Message.CodingKeys.content)
                    let message = Message(header: Header(role: Role(rawValue: role) ?? Role.system), content: content)
                    let finishReason = try choiceContainer.decode(String.self, forKey: .finish_reason)
                    let index = try choiceContainer.decode(Int.self, forKey: .index)
                    let choice = Choice(messages: [message], finishReason: finishReason, index: index)
                    choices.append(choice)
                }
                self.choices = choices
       
//        self.choices = try container.decode([Choice].self, forKey: .choices)
// print("Decoded mearly everything")
           let timestamp = try container.decode(Int.self, forKey: .created)
        self.created = Date(timeIntervalSince1970: TimeInterval(timestamp))
       }
}

MrDevel0per avatar Apr 12 '23 23:04 MrDevel0per

I am mostly interested in generating tests for Python scripts/libraries and Swift applications/libraries. I would likely want to generate a couple of tests at once - one file of tests, which can contain several, as I would want whatever I was generating the tests for to be 100% covered (or close to it).

MrDevel0per avatar Apr 12 '23 23:04 MrDevel0per